php 使用 yii 中的条件检索 CActiveDataProvider
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10665762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve a CActiveDataProvider with a criteria in yii
提问by Addev
I'm starting with yii and I have some trouble when trying to filter an CActiveDataProvider with a criteria.
我从 yii 开始,在尝试使用条件过滤 CActiveDataProvider 时遇到了一些麻烦。
This is my database model:
这是我的数据库模型:
Table Columns
Project [id, name, status]
userToProject [user_id,project_id,role]
User [id , ....]
And I'd like the indexaction to show all the projects with status=finished or where the user is currently assigned to that project.
而且我希望索引操作显示状态为已完成的所有项目或用户当前分配给该项目的位置。
So hard-coding the user id for testing purposes. This is my code:
因此,为了测试目的,对用户 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,
));
But it throws an Exception saying Unknown column 'user.id. What am I missing? Thanks
但它抛出一个异常,说Unknown column 'user.id。我错过了什么?谢谢
Edit: The error code is:
编辑:错误代码是:
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
回答by dInGd0nG
Try this
尝试这个
$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,
));

