0

当我添加$criteria->with=array('profile');字段时,revent_datetime总是返回 NULL,但没有返回权。为什么会发生标准?withrevent_datetimewith

控制器.php

public function actionIndex()
{
    $dayofweek = date('w');
    $criteria = new CDbCriteria;
    $criteria->select=array("
        CASE 
            WHEN weekday >= $dayofweek
            THEN CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', event_time )
            ELSE CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', event_time )
        END as revent_datetime,

        CASE 
            WHEN weekday >= $dayofweek AND list_time IS NOT NULL
            THEN CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', list_time )
            ELSE CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', list_time )
        END as rlist_datetime
    ");

    $criteria->with=array('profile'); // Thats the line w/ problem.

    $dataProvider=new CActiveDataProvider('Eventsreminder', array(
            'criteria' => $criteria
    ));

    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

输出:

$dataProvider->profile->name = Profile Test
$dataProvider->revent_datetime = NULL // here is the problem
$dataProvider->rlist_datetime = 2013-01-11 20:00:00
4

1 回答 1

3

好吧,如果你在 select 中使用函数,你必须创建 CDbExpression:

$criteria->select=array(
    new CDbExpression("CASE 
        WHEN weekday >= $dayofweek
        THEN CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', event_time )
        ELSE CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', event_time )
    END as revent_datetime"),
    new CDbExpression("CASE 
        WHEN weekday >= $dayofweek AND list_time IS NOT NULL
        THEN CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', list_time )
        ELSE CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', list_time )
    END as rlist_datetime")
);
于 2013-01-11T05:26:07.007 回答