[英]how can i use variable in where condition ( in clause) in yii2
I am trying to filter user records from user
table.我正在尝试从
user
表中过滤用户记录。 I have a user_id
string like eg 7,8,9, its working fine when I put that user_id
string directly in when condition like this我有一个
user_id
字符串,例如 7,8,9,当我把那个user_id
字符串直接放入这样的条件时,它工作正常
$model = User::find()
->select(['id','username','first_name','last_name','image','year','city'])
->distinct(true)
->where(['IN','id',[ 7,8,9 ]])
->asArray()
->all();
but when I pass this user_id
string in a variable, its show me only one record,但是当我在变量中传递这个
user_id
字符串时,它只显示一条记录,
$userFiltr = ArrayHelper::getColumn($result,'user_id');
$user_id = implode(',',$userFiltr); // $user_id return 7,8,9
$model = User::find()
->select(['id','username','first_name','last_name','image','year','city'])
->where(['IN','id',[ $user_id ]]) // when is user this variable $user_id, then query return only one result, white if i put 7,8,9 directly in where(['IN','id',[ 7,8,9 ]]) it return correct result
->asArray()
->all();
problem is that when is user this variable $user_id, then query return only one result, white if i put 7,8,9 directly in where(['IN','id',[ 7,8,9 ]]) it return correct result .问题是当用户这个变量 $user_id 时,查询只返回一个结果,如果我把 7,8,9 直接放在 where(['IN','id',[ 7,8,9 ]]) 它返回正确的结果。
how can i use variable in where condition like where(['IN','id',[ $user_id ]]) please help,thanks in advance我如何在 where(['IN','id',[ $user_id ]]) 这样的条件下使用变量,请帮忙,提前致谢
As $userFiltr
is already an array , pass it to where
:由于
$userFiltr
已经是一个数组,将它传递给where
:
$userFiltr = ArrayHelper::getColumn($result,'user_id');
$model = User::find()
->select(['id','username','first_name','last_name','image','year','city'])
->where(['IN','id',$userFiltr])
->asArray()->all();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.