yii:CDbCriteria打印出错误的结果

时间:2012-11-18 16:46:15

标签: php yii

问题:CDbCriteria打印出错误的结果。

这是模型搜索功能。

public function summary()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        $criteria->select = 't.*';

        if (Yii::app()->user->checkAccess('Reviewer')):
            $criteria->addSearchCondition('t.reviewers',Yii::app()->user->id,false, 'AND');
        endif;

        if (Yii::app()->user->checkAccess('Implementer')):
            $criteria->addSearchCondition('t.implementers',Yii::app()->user->id,false, 'OR');
        endif;

        if (Yii::app()->user->checkAccess('Creator')):
            $criteria->compare('t.creator',Yii::app()->user->id, false, 'OR');
        endif;

        $criteria->compare('request_type_id',$this->request_type_id);
        $criteria->compare('request_category_id',$this->request_category_id);
        $criteria->compare('financial_type_id',$this->financial_type_id);
        $criteria->compare('urgency_id',$this->urgency_id);
        $criteria->compare('eta_date',$this->eta_date,true);

        if (Yii::app()->user->checkAccess('Reviewer') || Yii::app()->user->checkAccess('Implementer')):
            $criteria->join   = "JOIN profiles p ON (p.user_id = t.creator)";
            if($this->creator):
                $criteria->addSearchCondition('p.firstname',$this->creator, true, 'AND');
            endif;
        endif;

        if(isset($this->status)):
            $criteria->compare('t.status',$this->status);
        endif;



        $data = new CActiveDataProvider(get_class($this), array(
                'criteria'=>$criteria,
                'pagination'=>false,
        ));
        $_SESSION['all'] = $data;


        $data = new CActiveDataProvider(get_class($this), array(
                'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
                        Yii::app()->params['defaultPageSize']),),
                'criteria'=>$criteria,
        ));
        $_SESSION['limited'] = $data; 

        return $data;
    }
当我用CWebLogRoute跟踪时,上面的代码打印出了查询,但是没有记录。

  

查询SQL:SELECT t。* FROM request t JOIN配置文件p ON   (p.user_id = t.creator)WH。t.reviewers LIKE:ycp1 LIMIT 10

当我将这些查询更改为后续查询时,它会打印出记录。

  

SELECT t。* FROM request t JOIN配置文件p ON(p.user_id =   t.creator)在哪里。评论者喜欢'%3%'限制0,30

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试更改

$criteria->addSearchCondition('t.reviewers',Yii::app()->user->id,false, 'AND');

$criteria->addSearchCondition('t.reviewers',Yii::app()->user->id, true, 'AND');

同时检查reviewers的格式及其在记录中的记录方式:它可能记录为'3','3'或'3'。

必须按update request set reviewers = trim(reviewers)

更新记录