我想在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中编写上述查询。
答案 0 :(得分:2)
从错误的外观中你需要做的就是调整你用作表别名的名称
'condition'=>'user.user_id=:user_id AND YEAR(leaves.leave_from_date)= YEAR(NOW())',
^--------- change to t