T
T
Tatiana Komarova2015-09-04 09:05:45
Yii
Tatiana Komarova, 2015-09-04 09:05:45

How to form an ActiveQuery YII2 query?

I need to organize a request like:

SELECT * FROM table WHERE pole=value AND (pole1=value1 OR pole2=value2)

Not quite sure how to do this with andFilterWhere and orFilterWhere.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Talgat Baltasov, 2015-09-04
@tatia_kom

Table::find()->where(['and', 'pole=value', ['or', 'pole1=value1', 'pole2=value2']])->findAll()

K
Konstantin Shirkin, 2015-09-04
@skw85

Possible variant:

$query->where('pole = :value and (pole1 = :value1 or pole2 = :value2)');
$query->params([':value'=>"value", ':value1' => 'value1', ':value2' => 'value2']);

It is also possible to write something like this:
$query->params([':value'=>"value", ':value1' => 'value1', ':value2' => 'value2']);
$query->Where(['and', 'value = :value', ['or', 'value1 = :value1', 'value2 = :value2']]);

Operator Format in Query Builder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question