1

我已经按照 Yii cron 设置指令通过调用命令来配置作业(在 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'   
    ),
 ), 

测试命令.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();第一封邮件进行查询时,正在发送第二封邮件(在 DB 查询之后)。

编辑2

我从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 支持。有什么解决办法?

4

1 回答 1

0

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.

于 2013-10-22T09:55:21.377 回答