Doubt with activeQuery

I can’t believe I have a question like this, and at this point. Please if someone can help me.
// $criteria is any object ActiveQuery for example:
$criteria=Mymodel::find()->andWhere([‘myfield’=>2]);
//I want to keep original filters conditions from $criteria
// for this I store $criteria in other variable named : $originalQuery
$originalQuery=$criteria;
//Then I perform other tasks with $criteria variable for example :
$criteria->andWhere([‘myOtherField’=>3]);
Then I realize the following: When I check the original variable $originalQuery with the function createCommand()->rawSql. Turns out it also updates the filters added to $criteria, so:
echo $originalQuery->createCommand()->rawSql;
This It also contains the last filters added to $criteria.
Is this possible, or am I getting confused?

ActiveQuery is not immutable, so you need $originalQuery = clone $criteria;

1 Like