A
A
AlexSer2020-11-23 14:14:12
Yii
AlexSer, 2020-11-23 14:14:12

Yii2 ActiveRecord problem with SQL queries?

Hello!!! I make queries with counting the number of rows, and I noticed that the SQL queries seem to be connected.
For example I created

$query=Custum::find()->joinWith('order')->where(['id'=>101])
$count_custom=$query->count();

If I need to add a condition accordingly, I simply add andWhere()->count();
But I noticed that if I enter a new parameter with the same $query, for some reason it displays the added andWhere, so my calculations are wrong. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v3shin, 2020-11-23
@v3shin

$query = Custum::find()->joinWith('order')->where(['id'=>101]); // возвращает ActiveRecord с условием where
$count_custom = $query->count(); // считает с условием where
$query->andWhere(условие); // добавляет $query условие andWhere, теперь в нем два условия: where и andWhere

Are you asking about this?
UPD:
$query->andWhere()changes the $query object. If you need to apply different conditions, use the (clone $query)->andWhere().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question