Tell me, who is well acquainted with Yii2, is it possible using ActiveSearch to implement a search by keyword. I have found only this way so far, but I don’t really like it:

// Получаем названи таблиц $couponsTable = Coupons::tableName(); $clubProfileTable = ClubProfile::tableName(); // Поиск по ключевому слову $query->andFilterWhere([ 'like', 'concat(' . "`$couponsTable`.title, `$couponsTable`.description, " . "`$clubProfileTable`.city, `$clubProfileTable`.address" . ')', $this->keyword ]); 

  • In yii2, there is no ActiveSearch entity. In the framework, there are ORMs for Sphinx Search and Elasticsearch , which are inherited from the base BaseActiveRecord , and you can also use DBAL / Query builder instead of ORM ActiveRecord . While I see that you use stupidly "LIKE", and preferably full-text search. What is your RDBMS, MySQL? - romeo
  • Sphinx Search created by Andrei Aksyonov and Elasticsearch are search engines (indexers) that can be paired with DBMS (MySQL, PostgreSQL, MongoDB, etc.). In general, you need skill in setting them up . - romeo

1 answer 1

Without concatenation:

 $query->orWhere(['like', "{{{$couponsTable}}}.[[title]]", $this->keyword]) ->orWhere(['like', "{{{$couponsTable}}}.[[description]]", $this->keyword]) ->orWhere(['like', "{{{$clubProfileTable}}}.[[city]]", $this->keyword]) ->orWhere(['like', "{{{$clubProfileTable}}}.[[address]]", $this->keyword]) 

With concatenation:

 $query->andWhere(['like', "CONCAT ({{{$couponsTable}}}.[[title]], {{{$couponsTable}}}.[[description]], {{{$clubProfileTable}}}.[[city]], {{{$clubProfileTable}}}.[[address]])", $this->keyword]]) 

Screening of table and column names in ORM / DBAL yii2 is done: {{<имя_таблицы>}} and [[<имя_столбца>]] . This is done for easy migration / porting to other DBMS.

As soon as you tell me what is your DBMS (I see only that it is not postgres), let's see what can be done with full-text search.

  • I am using DBMS - MySQL - dima-opr