简体   繁体   中英

DB connection does not work in Yii cronjob

I've followed the Yii cron setup instruction for configuring jobs thru invoking Commands (Run PHP in CLI (console) mode). The cron job works as designed, yet when i address to the AR models or query SQL the script/command doesn't work.

I've set the DB connection in config/cron.php :

...
'components'=>array(
    ...
    'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=blogandt_yiiapp',
        //mysql:host=127.0.0.1;port=3306;
        'emulatePrepare' => true,
        'username' => '...',
        'password' => '..',
        'charset' => 'utf8',
        'tablePrefix' => 'yiiapp_', 
        'class'         => 'CDbConnection'   
    ),
 ), 

TestCommand.php:

<?php
class TestCommand extends CConsoleCommand {
    public function run($args) {        
    $dateObj = new DateTime('NOW');  //date("Y-m-d");       
    $fromTime = date_format($dateObj, "Y-m-d H:m:i");
            date_modify($dateObj, '+5 days');   
    $toTime = date_format($dateObj, "Y-m-d H:m:i"); 
    $message = "Time span is from {$fromTime} to {$toTime} ";
    mail('xyz@gmail.com', 'TestCommand run', $message, '');

    $criteria = new CDbCriteria;
    $criteria->addBetweenCondition('time', $fromTime, $toTime);  
    $notification = DocEventNotification::model()->findAll($criteria);
            // OR
    //$query = 'SELECT n.UserId, n.EventId FROM yiiapp_doc_event_notification n';
    //$query .= "WHERE (n.time BETWEEN '{$fromTime}' AND '{$toTime}') AND n.turn = '1' ";
    //$rows = Yii::app()->db->createCommand($query)->queryAll();
    mail('xyz@gmail.com', 'TestCommand after DB query', $message, '');
      }
  } ?>

When i do not address the DB whether thru AR or directly the emails are sent, while with DB queries - are not. I've checked these AR queries in a controlller - they work fine. What's wrong?

EDIT

Actually when querying thru $rows = Yii::app()->db->createCommand($query)->queryAll(); the 1st mail is being sent, while second one (after DB query) is not.

Edit2

I put down a snippet from cron.log . Seems there is a problem with PDO object.

 2013/10/21 23:25:00 [error] [php] include(PDO.php): failed to open stream: No such file or       directory (/home/blogandt/domains/blogandtraffic.com/public_html/framework/YiiBase.php:427)

I've not included PDO support while configuring Yii. What's solution?

It could be a problem of concatenation. Was missing a space after the n

$query = 'SELECT n.UserId, n.EventId FROM yiiapp_doc_event_notification n ';
$query .= "WHERE (n.time BETWEEN '{$fromTime}' AND '{$toTime}') AND n.turn = '1' ";

Try and tell us what happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM