Лучший подход в соответствии с запросом MYSQL с использованием CONCAT()
, как показано ниже
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