php – 使用yii中的条件检索CActiveDataProvider

前端之家收集整理的这篇文章主要介绍了php – 使用yii中的条件检索CActiveDataProvider前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我从yii开始,在尝试使用条件过滤CActiveDataProvider时遇到了一些麻烦.

这是我的数据库模型:

Table            Columns
Project          [id,name,status]
userToProject    [user_id,project_id,role]
User             [id,....]

我希望索引操作能够显示status = finished或用户当前分配给该项目的所有项目.

因此,为了测试目的,硬编码用户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
最佳答案
试试这个

$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,));
原文链接:https://www.f2er.com/mysql/433885.html

猜你在找的MySQL相关文章