如何用“IN(1,2,3)”使用的findAll? [英] How to use 'IN (1,2,3)' with findAll?
问题描述
我需要一对夫妇从数据库中的学生,我有自己的主键在一个逗号分隔的字符串。
I need to get a couple of Students from the database, and I have their primary keys in a comma-separated string.
通常使用SQL它会是这样的:
Normally using SQL it would be something like:
$cleanedStudentIdStringList = "1,2,3,4";
SELECT * FROM Student WHERE id IN ($cleanedStudentIdStringList)
Yii中的ActiveRecord的似乎是插入一个单引号,在最终的SQL语句各地势必参数,导致查询使用参数绑定时失败。
Yii's ActiveRecord seems to insert a single quote around bound parameters in the resulting SQL statement which cause the query to fail when using parameter binding.
这工作,但不使用安全参数绑定。
This works, but doesn't use safe parameter binding.
$students = Student::model()->findAll("id IN ({$_POST['studentIds']})");
有没有一种方法能继续使用参数绑定,并获得唯一的一对夫妇排在单个查询?
Is there a way to still use parameter binding and get only a couple of rows in a single query?
推荐答案
您可以做同样的方式:
$criteria = new CDbCriteria();
$criteria->addInCondition("id", array(1,2,3,4));
$result = Student::model()->findAll($criteria);
和在阵列中使用任何你需要的值。
and use in array any values you need.
Aleksy
这篇关于如何用“IN(1,2,3)”使用的findAll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!