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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines