我从yii开始,在尝试使用条件过滤CActiveDataProvider时遇到了一些麻烦。
这是我的数据库模型:
Table Columns
Project [id, name, status]
userToProject [user_id,project_id,role]
User [id , ....]
我希望索引操作能够显示状态=已完成的所有项目或用户当前分配给该项目的所有项目。
为了测试目的,对用户ID进行硬编码。这是我的代码:
$criteria=array(
'order'=>'status desc',
'with'=>array(
'userToProject','userToProject.user'=>array('alias'=>'user')),
);
$criteria['condition']='status=='.Project::STATUS_FINISHED;
$criteria['condition'].=' OR user.id = 6';
$dataProvider=new CActiveDataProvider('Project', array(
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
但它引发了一个例外未知列'user.id 。我错过了什么?感谢
编辑:错误代码为:
Column not found: 1054
Unknown column 'users.user_id' in 'where clause'. The SQL statement executed was:
SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`description` AS `t0_c2`,
`t`.`status` AS `t0_c3`, `t`.`creation_date` AS `t0_c4` FROM `pgp_project` `t` WHERE
(status=4 OR users.user_id=6) ORDER BY status desc LIMIT 10
答案 0 :(得分:14)
试试这个
$criteria=new CDbCriteria(array(
'order'=>'status desc',
'with' => array('userToProject'=>array('alias'=>'user')),
'condition'=>'status='.Project::STATUS_FINISHED.' OR user.id = 6',
));
$dataProvider=new CActiveDataProvider('Project', array(
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));