N
N
Nikolai Novosad2015-12-05 23:13:05
Yii
Nikolai Novosad, 2015-12-05 23:13:05

Yii2 compose query via LIKE?

Hello.
You need to make a query like this:

SELECT * FROM news WHERE date_news LIKE '____-$var-__';

I write like this:
$events = News::find()
                ->where(['LIKE', 'date_news', '____-' . $incident . '-__'])
                ->all();

In this case, nothing comes out. In yii-debug this is:
SELECT * FROM `news` WHERE `date_news` LIKE '%\\_\\_\\_\\_-12-\\_\\_%'

How to change?
I can write like this
$events = News::find()
                ->where("date_news LIKE '____-$incident-__' ")
                ->all();

And everything will work, but I want to write the request correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sunrails, 2015-12-06
@nik_neman

If so?

$events = News::find()
                ->where(['LIKE', 'date_news', '____-' . $incident . '-__', false])
                ->all();

www.yiiframework.com/doc-2.0/yii-db-query.html#whe...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question