I receive the following error when I use ‘addInCondition’ to the CDbCriteria object:
This only occurs when I already have a condition already specified; it works fine if I do not add any additional conditions. Does anyone else have this issue? Has anyone gotten around it.
Trying to reproduce the error I get this in the stack trace:
Error in querying SQL: SELECT * FROM `MyModel` `t` WHERE (active=:active) AND
(id NOT IN (:ycp0, :ycp1)) ORDER BY t.id DESC LIMIT 1. Bind with parameter
:active=1
I used the code provided by chazclark. For some reasons Yii replaces the actual values given in the array (array(1,2)) with tokens (:ycp0, :ycp1). If we add these tokens to params everything works as expected. Here:
Thanks for looking at this. I attempted using the compare() method yet I am getting the same error. Please let me know if I’m using it incorrectly as I’ve never used it before:
Equ, thanks for your suggestion; however, although it does work as suggested in this example, the array will be populated with a dynamic array so I’m not sure that it will work in my use case.
Querying SQL: SELECT * FROM `dba`.`MyModel` `t` WHERE (active=:active) AND (id IN (:ycp0, :ycp1)) ORDER BY t.id DESC LIMIT 1. Bind with parameter :active=1 in [...]MyModelController.php (104) in [...]index.php (12)
Turns out that if I change the order and place the params property before the “addInCondition” or “compare()” method, I’ll get the correct query run. Here’s the code change I made for anyone else with the same problem:
$criteria->compare will add both a part of condition and param. Generally there is no need to bind params manually but if you still want to do so it’s easy to do with native PHP array syntax: