M
M
Max DangerPro2016-11-07 16:48:32
Yii
Max DangerPro, 2016-11-07 16:48:32

How to make SQL query with LIKE in YII2?

Doing a site search with LIKE? it turns out this line:

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

This is a search only on the name field , but how can I make the search be on several fields. For example name, content, keywords, description?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-11-07
@DangerPro

Course::find()->where(['like', 'name', $q])
    ->orWhere(['like', 'content', $q])
    ->orWhere(['like', 'keywords', $q])
    ->orWhere(['like', 'description', $q])

In general, if you are doing a search on the site (and judging by the text of the question, you are doing it), for example, use full-text search , and even better - search engines like sphinx

M
Max, 2016-11-07
@MaxDukov

andWhere
$query = Course::find()->where(['like', 'name', $q])
$query = Course::find()->andWhere(['like', 'content', $ cont])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question