Yii 使用带 IN 条件的变量 [英] Yii using a variable with an IN condition

查看:53
本文介绍了Yii 使用带 IN 条件的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的模型将信息提取到页面中.问题是我需要使用变量在我的 mysql 上使用 IN 条件.

I am trying to pull information into a page using my model. The issue is that I need to use an IN condition on my mysql using a variable.

这是我目前使用的代码

$list_id = '1,3';
$clients = ListSubscriber::model()->findAll(array('condition'=>'list_id IN (:list_id)','params'=>array(':list_id'=>$list_id)));

我不一定知道 $list_id 中将存储多少个数字,因此需要一个变量来处理 IN.

I won't necessarily know how many numbers will be stored within $list_id, hence the need for a variable to work with the IN.

代码确实执行没有错误,但似乎只返回了 $list_id 的第一个数字的值,所以在这种情况下,它只找到 list_id 的用户= 1.

The code does execute without errors, but only seems to return the values for the first number of $list_id, so in this case it only finds users where the list_id = 1.

感谢任何帮助.我发现了这个问题 Yii addInCondition但是他们使用的是静态值,这不能解决我的问题.

Any help is appreciated. I have found this question Yii addInCondition However they are using static values, which does not resolve my issue.

当我确实使用静态值时,代码会按照预期的结果执行.

When I do use static values, the code executes with results as expected.

推荐答案

您可以使用 addInCondition :

You can use addInCondition :

$list_id = '1,3';
$criteria = new CDbCriteria();
$arr_list_id = explode(",",$list_id);
$criteria->addInCondition("list_id ", $arr_list_id );
$clients = ListSubscriber::model()->findAll($criteria);

这篇关于Yii 使用带 IN 条件的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆