Php 是否可以在yii比较的条件下使用
您好,我正在尝试使用yiiPhp 是否可以在yii比较的条件下使用,php,yii,Php,Yii,您好,我正在尝试使用yiiCDbCriteria创建搜索功能,一切正常。但当我输入全名时,即manoj dhiman我已将其保存为数据库中的firstname和lastname。所以我分解搜索的键,所以我需要类似的东西,这样我就可以用该数组的comper搜索结果。我的代码是 public function actionSearchuser() { $key=$_POST['key']; $data=array(); if($key)
CDbCriteria
创建搜索功能,一切正常。但当我输入全名时,即manoj dhiman
我已将其保存为数据库中的firstname
和lastname
。所以我分解搜索的键,所以我需要类似的东西,这样我就可以用该数组的comper搜索结果。我的代码是
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)代码>但正在将整个键与名称匹配。我想要像
一样的。这怎么可能
感谢《疯狂骷髅》的评论,它对我有用<代码>$criteria->compare('concat(firstname,“,lastname)”,$key,true,'OR')代码>
可能相关:参考:@Inaseskull谢谢。
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));
}