reenomt2
(Reenomt2)
1
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 ?
Javache
(Pieter Debaets)
2
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.
Coksnuss
(Schanz15)
3
I would prefer CActiveRecord::findAllByAttributes()
[PHP]Users::model()->findAllByAttributes(array(‘id’ => $userIds))[/PHP]
reenomt2
(Reenomt2)
4
Thanks guyz, this is exactly what I was looking for.