Jump to content
shivsingh7150

Insert to DB with Hook not working

Recommended Posts

Hello

i am want to insert some value every day to a extra table created by me. so i am doing it by hook as below below noting doing.

 

$filePre = date('Y-m-d', strtotime(' -1 day'));      ///// One day old Date
$updated_at = $created_at = date('Y-m-d H:i:s');     ///// Current date
$extension = '.zip';
    
$nameArray = ('', 'domain', 'india', 'us'); ///// Add more database file name string after $filePre

$test = '';    
    
foreach ($nameArray as $fileName) {
    $test .= "$fileName <br>";


add_hook('PreCronJob', 1, function($database) {
    
$filePre = date('Y-m-d', strtotime(' -1 day'));      ///// One day old Date
$created_at = date('Y-m-d H:i:s', strtotime(' -1 day'));     ///// Current date
$updated_at = date('Y-m-d H:i:s');      ///// Current date
    
$extension = '.zip';
    
$nameArray = ("4"=>"", "2"=>"domain", "1"=>"india", "7"=>"us"); ///// Database tyep with file name
    
foreach ($nameArray as $whois_type => $fileName) {
    
$whois_file = $filePre.''.$fileName.''.$extension;
    
use WHMCS\Database\Capsule;

try {
    Capsule::connection()->transaction(
        function ($connectionManager)
        {
            /** @var \Illuminate\Database\Connection $connectionManager */
            $connectionManager->table('whois_database')->insert(
                [
                    'whois_file' => $whois_file,
                    'whois_status' => 0,
                    'whois_type' => $whois_type,
                    'created_at' => $created_at,
                    'updated_at' => $updated_at,
                ]
            );
        }
    );
    echo 'cron test';
} catch (\Exception $e) {
    echo "Uh oh! Inserting didn't work, but I was able to rollback. {$e->getMessage()}";
}    
    
}    
    
});

.

Share this post


Link to post
Share on other sites

hello

i have fixed it myself so please close this topic. my working code:

 

<?php

add_hook('PreCronJob', 1, function($vars) {
    
$filePre = date('Y-m-d', strtotime(' -1 day'));      ///// One day old Date
$created_at = date('Y-m-d H:i:s', strtotime(' -1 day'));     ///// Current date
$updated_at = date('Y-m-d H:i:s');      ///// Current date
 
$extension = '.zip';
    
$nameArray = array("4"=>"", "2"=>"-domain", "1"=>"-india", "7"=>"-us"); ///// Database tyep with file name   

 
foreach ($nameArray as $whois_type => $fileName) { 

$whois_md5 = md5(generateRandomString());
    
$whois_file = $filePre.''.$fileName.''.$extension;
    
try {
     Illuminate\Database\Capsule\Manager::table("whois_database")->insert(array(
         "whois_md5" => $whois_md5,
         "whois_file" => $whois_file,
         "whois_status" => '0',
         "whois_type" => $whois_type,
         "created_at" => $created_at,
         "updated_at" => $updated_at));
       
    } catch (Exception $e) {
        return array("status" => "error", "description" => "Unable to create table whois_database: " . $e->getMessage());
    }
}
    
});

 

Thanks for attention 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated