是否可以在yii比较

时间:2015-10-12 07:33:58

标签: php yii

您好我正在尝试使用yii CDbCriteria创建搜索功能,一切正常。但是当我输入全名.i.e manoj dhiman时,我已将其保存为数据库中的firstnamelastname。所以我爆炸搜索到的键,所以我需要这样的东西,以便我可以用该数组的主持人搜索结果。我的代码是

public function actionSearchuser()
    {
        $key=$_POST['key'];
        $data=array();
        if($key)
        {
            $users=explode(' ',$key);   // the users array 
            $model=new Goals;
            $model->attributes = $_POST['key'];
            $criteria = new CDbCriteria();
            $criteria->with='profile';
            $criteria->distinct=true;

            //$criteria->addInCondition('firstname',$users);
            $criteria->compare('firstname', $key, true, 'OR');
            $criteria->compare('school', $key, true, 'OR');
            $criteria->compare('occupation', $key, true, 'OR');
            $criteria->compare('location', $key, true, 'OR');
            $criteria->compare('lastname', $key, true, 'OR');
            $criteria->compare('username', $key, true, 'OR');
            $criteria->compare('email', $key, true, 'OR');


             $dataProvider=new CActiveDataProvider('User', array(
                'criteria' => $criteria
        ));

            $data=$dataProvider->getData();
        }
        $this->renderPartial('Searchuser',array('data'=>$data));
    }

我也尝试了$criteria->addInCondition('firstname',$users);,但是将整个键与名称匹配。我想要like。怎么可能?

由于

1 个答案:

答案 0 :(得分:1)

在疯狂头骨的评论之后它对我有用。 $criteria->compare('concat(firstname, " ", lastname)', $key, true, 'OR'); reference

public function actionSearchuser()
    {
        $key=$_POST['key'];
        $data=array();
        if($key)
        {
            $users=explode(' ',$key);
            $model=new Goals;
            $model->attributes = $_POST['key'];
            $criteria = new CDbCriteria();
            $criteria->with='profile';
            $criteria->distinct=true;

            //$criteria->addInCondition('firstname',$users);
            $criteria->compare('firstname', $key, true, 'OR');
            $criteria->compare('concat(firstname, " ", lastname)', $key, true, 'OR');
            $criteria->compare('school', $key, true, 'OR');
            $criteria->compare('occupation', $key, true, 'OR');
            $criteria->compare('location', $key, true, 'OR');
            $criteria->compare('lastname', $key, true, 'OR');
            $criteria->compare('username', $key, true, 'OR');
            $criteria->compare('email', $key, true, 'OR');


             $dataProvider=new CActiveDataProvider('User', array(
                'criteria' => $criteria
        ));

            $data=$dataProvider->getData();
        }
        $this->renderPartial('Searchuser',array('data'=>$data));
    }