select where in

hi

i dont know if this is a bug or feature but it does not work for me:

$inquery = "(1, 2, 4)";

$criteria->condition='id IN :inquery'; 

$criteria->params=array('inquery'=>$inquery);

$barList = Bar::model()->findAll($criteria);

CDbException

Description

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''(1, 2, 4)'' at line 1

a had to do

$criteria->condition='id IN {$inquery}';

i think its not good solution, so what is preffered way to do such selection ?

You can't bind parameters this way (not allowed by PDO).

You can make use of the following snippet to create a IN condition:



<?php


$db=Bar::model()->dbConnection;


$condition=$db->commandBuilder->createInCondition(Bar::model()->tableSchema, 'id', $values);


Bar::model()->findAll($condition);


Thanks!, maybe you should put this lines in cookbook.

one more thing,

when $values is array([1]=>1) query is bar.id IS NULL

i need 2 elements to work properly:

Array ( [1] => 1 [2] => 1 )  then bar.id IN (1, 1)

Use array_values($values) (to reset the index to 0-based).