Cactiverecord::findall

I’m trying to perform a search in one of my tables based on a given criteria like so:




$id = 1;

$criteria = new CDbCriteria();

$criteria->addCondition("usr_currency=:currency");

$currencies = User::model()->findAll($criteria, array(':currency' => $id,)); 		



I get a CDbException:




CDbCommand failed to execute the SQL statement: 

SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. 

The SQL statement executed was: 

	SELECT * FROM `user` `t` 

	WHERE usr_currency=:currency



Where as, this works:




$id = 1;

$criteria = new CDbCriteria();

$criteria->addCondition("usr_currency=:currency");

$criteria->params = array(':currency' => $id,);

$comments = User::model()->findAll($criteria); 		



What is wrong with the first code fragment?

You answer yourself, the correct answer is the second code.

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#params-detail

Second argument ($params) is used only when the first argument ($criteria) is a string.

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#find-detail

http://www.yiiframework.com/doc/api/1.1/CDbCommandBuilder#createCriteria-detail

For example, this should work:


$currencies = User::model()->findAll('usr_currency=:currency', array(':currency' => $id,));