数据库连接在Yii cronjob中不起作用

时间:2013-10-22 08:47:15

标签: php mysql database yii cron

我已经按照Yii cron setup instruction通过调用命令配置作业(在CLI(控制台)模式下运行PHP)。 cron作业按设计工作,但当我解决AR模型或查询SQL时,脚本/命令不起作用。

我在 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, '');
      }
  } ?>

当我没有通过AR或直接发送电子邮件来解决数据库时,使用数据库查询 - 不是。我在控制器中检查了这些AR查询 - 它们工作正常。怎么了?

修改

实际上,当通过$rows = Yii::app()->db->createCommand($query)->queryAll();查询第一封邮件时,第二封邮件(在数据库查询之后)不是。

EDIT2

我放下cron.log的片段。似乎PDO对象存在问题。

 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)

配置Yii时我没有包含PDO支持。 什么是解决方案?

1 个答案:

答案 0 :(得分:0)

这可能是连接问题。在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' ";

尝试告诉我们发生了什么。