bug with binding params to Mysql IN statement

If you try to do something like this:

User::model()->findAllBySql("… AND UserType IN :userTypes…", array(’:userTypes’=>"(1,2,3)"));

You get an error because the binding puts quotes arround the brackets: e.g.

AND UserType IN "(1,2,3)"

instead of

AND UserType IN (1,2,3)

Any way to get round this?

You can’t bind this way. It is not supported by PDO. You have to bind one by one.

You may also consider using: $db->commandBuilder->createInCondition()

Seems to work if you move the parentheses outside the value being bound.

I.e instead of

User::model()->findAllBySql("… AND UserType IN :userTypes…", array(’:userTypes’=>"(1,2,3)"));

THIS DOES WORK

User::model()->findAllBySql("… AND UserType IN (:userTypes)…", array(’:userTypes’=>“1,2,3”));

Actually this doesn’t work - you need Yii::app()->db->schema->commandBuilder->createInCondition()