Yii在加入后忽略自定义字段

时间:2013-01-11 02:14:07

标签: php mysql yii

当我添加$criteria->with=array('profile');时,字段revent_datetime总是返回NULL, 但没有with revent_datetime返回权限。 为什么会出现with条件?

Controller.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

1 个答案:

答案 0 :(得分: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")
);