yAnTar
yAnTar

Reputation: 4610

Troubles with IN in Yii query builder

I use Yii Framework and i need to build difficult query with many conditions. I'm filling 2 arrays $conditions and $values. And i have one problem. Below is example When i use

$usersId = '1,2';
$conditions[] = 'e.user_id IN(:usersId)';
$values[':usersId'] = $usersId;

I get only value from user_id = 1

When i'm not use option and write manually

$usersId = '1,2';
$conditions[] = 'e.user_id IN(' . $usersId . ')'; 

no problem.

Of course i can use second construction, but it seems not very good.

Upvotes: 0

Views: 481

Answers (3)

Imre L
Imre L

Reputation: 6249

Yii way would be to use CDbCriteria addInCondition function

$usersId = array(1,2); //must be array
$criteria=new CDbCriteria();
$criteria->addInCondition('user_id',$usersId);

$result = MyModel::model()->findAll($criteria);

Upvotes: 2

Rajat Singhal
Rajat Singhal

Reputation: 11264

You should addInCondition

$criteria->addInCondition('e.user_id',array(1,2));

Upvotes: 2

Diane
Diane

Reputation: 119

$values[':usersId'] = $usersId;

If I understand your wuestion correctly, you can use the BindParam function in yii?

Instead of this - $values[':usersId'] = $usersId;

Write this - $command->BindParam(':usersId', $usersId, PDO::PARAM_STR);

Very simply, you're binding your parameters to your command statement.

Hope it works!

Upvotes: 1

Related Questions