Yii way to use SQL IN() function

Hi,

Is there any Yii way to use sql IN()?

What I did for now is:




Users::model()->findAll('id IN(' . implode(',',$usersIds) . ')')

$usersIds is an array of IDs of course.

It works but anyway I’m not sure if this is the way I should do it with Yii ?

CDbCriteria supports specifying those.


$criteria = new CDbCriteria();

$criteria->addInCondition('id', $usersIds);

Users::model()->findAll($criteria);

Besides the fact that it looks better, it’s also a lot safer and protects you against SQL-injections.

I would prefer CActiveRecord::findAllByAttributes()

[PHP]Users::model()->findAllByAttributes(array(‘id’ => $userIds))[/PHP]

Thanks guyz, this is exactly what I was looking for.