1

我想在 yii 中编写以下查询:

SELECT * FROM tbl_leaves as tl,tbl_users_master as um
WHERE tl.user_id = um.user_id AND YEAR(tl.leave_from_date) = YEAR(NOW())

我试图在 yii 中编写上述查询,如下所示:

 $dataProvider=new CActiveDataProvider('Leaves', array(
        'criteria'=>array(
            'with'=>'user',
            'together'=>true,
            'condition'=>'user.user_id=:user_id AND YEAR(leaves.leave_from_date)= YEAR(NOW())',
            'params'=>array(':user_id'=>$this->loadModel(Yii::app()->user->getId())->user_id),              
        ),
    ));
     $this->render('admin',array('model'=>$this->loadModel(Yii::app()->user->getId()),
    'dataProvider'=>$dataProvider,
    ));

但它抛出了以下错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: 
Column not found: 1054 Unknown column 'leaves.leave_from_date' in 'where clause'.

执行的 SQL 语句是:

 SELECT COUNT(DISTINCT `t`.`leave_id`) FROM `tbl_leaves` `t` 
 LEFT OUTER JOIN `tbl_users_master` `user` ON (`t`.`user_id`=`user`.`user_id`) 
 WHERE (user.user_id=:user_id AND YEAR(leaves.leave_from_date) = YEAR(NOW()))

我是 yii 新手,请帮助我如何在 yii 中编写上述查询。

4

1 回答 1

2

从错误的外观来看,您需要做的就是调整您用作表别名的名称

'condition'=>'user.user_id=:user_id AND YEAR(leaves.leave_from_date)= YEAR(NOW())',
                                                ^--------- change to t
于 2013-08-01T07:52:09.690 回答