Z
Z
ZaurK2017-06-14 10:50:11
MySQL
ZaurK, 2017-06-14 10:50:11

How to make a search not by the exact occurrence of the search word?

Hello! I know that in yii2 it's better to use the sphynx library to search the site, but now I'm doing it manually with the following request:

$query = Pages::find()->where(['like', 'title', $q]);

This query returns an exact match, i.e. if you need to find "12345", then the query "123" will return nothing. In simple mysql, the query would be built something like this
$sql = "SELECT title FROM pages WHERE title LIKE '%" . $q . "%' ";

Here's how to enter these '%" . $q . "%' in a yii2 query?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
n-fom, 2017-06-14
@n-fom

How to write "left" and "right" LIKE in ActiveQuery Yii2?

I
Igor Vasiliev, 2017-07-23
@Isolution666

If you did it through the GRUD generator in gii, then you should have created a certain NameSearch class.
Connect to it in any way convenient for you, I used the DetailView and ListView widgets. In principle, skillfully, you can display information in any form. A minimum of code, numbering, pagination, recalculation of the number of displayed records, you can form the appearance as you want, such moments are already provided in the search class. I don’t know about others, for any coincidence I displayed infa, even one character at a time, at least 3 characters, it doesn’t matter)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question