在我的网站上,当我搜索阿历克斯·刘时,我会给出结果。问题是我如何修改代码,所以当我搜索刘亚力克斯的时候,它仍然会显示刘的结果。现在还没有。
$query->orFilterWhere(['like', 'fullname', $this->globalSearch])这是我的搜索模型中的代码。
请帮我处理这个。
谢谢!
发布于 2018-11-29 13:41:28
使用CONCAT()进行MYSQL查询的更好方法,如下所示
if( $this->fullname!== null && $this->fullname !== '' ){
$searchTerms = explode(" ", $this->fullname);
if( sizeof($searchTerms) > 1 ){
$conditions[]='OR';
foreach( $searchTerms as $term ){
$conditions[] = ['like', 'fullname', new \yii\db\Expression('CONCAT(CONCAT("%","' . $term . '"),"%")')];
}
$query->orFilterWhere($conditions);
} else{
$query->orFilterWhere(
['like', 'fullname', new \yii\db\Expression('CONCAT(CONCAT("%","' . $searchTerms[0] . '"),"%")')]
);
}
}注意:如果您正在以first last或first middle last__格式搜索名称,这意味着您可以搜索Alex Lau John或John Alex Lau Lau John Alex。
发布于 2018-11-28 07:45:05
尝尝这个
$words = explode(' ',$this->globalSearch);
if (count($words) > 1) {
foreach ($words as $word) {
$query->orFilterWhere(['like', 'fullname', $word]);
}
} else {
$query->orFilterWhere(['like', 'fullname', $this->globalSearch]);
}备注:,您需要确保空白
https://stackoverflow.com/questions/53514275
复制相似问题