[SOLVED] CDbCriteria condition "where column IN"

Hi

I want to use CDbCriteria to search model. According to reference:




$post=Post::model()->find(array(

    'select'=>'title',

    'condition'=>'postID=:postID',

    'params'=>array(':postID'=>10),

));



this code selects title column from Post table/model where postID=10.

How can I search model by "where postID in (1,2,3,4)"?

Any suggestions?

Hi, try:




$sIds = '1,2,3,4';


$post=Post::model()->find(array(

    'select'=>'title',

    'condition'=>'postID IN (:postID)',

    'params'=>array(':postID'=>$sIds),

));

Thanks,

It seems not to throw error, after my tryings. But anyway the variable $post is an array? How can I display it? If I print_r it or echo I get nothing rendered.


<?php $sIds = '1,2,3,4';


$posts=Post::model()->find(array(

    'select'=>'title',

    'condition'=>'postID IN (:postID)',

    'params'=>array(':postID'=>$sIds),

)); ?>


<?php foreach($posts as $post) : ?>

<?php echo $post->title; ?><br/>

<?php endforeach; ?>

Well looks like $posts is not an array. I followed your code, this foreach loop should loop through array elements and echo it. But I get error:

[size="5"][color="#FF0000"]PHP Error[/color][/size]

[b]

Description[/b]

Invalid argument supplied for foreach()

I carefully followed this code to satisfy my model and my column name and my comma seperated IDs, but still getting this error. Have you tried using this code for yourself?

find() returns one object, findAll() returns an array of objects.

/Tommy

Yes actually it should be [url=&quot;http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail&quot;]findAll/url if you want to get an array of multiple posts.

Ok

After I used findAll print_r($posts) now renders “Array()” in html, what means that this findAll haven’t returned any values?

Well after deep investigation I found that this works where postID=1:




<?php $sIds = '1';


$posts=Post::model()->findAll(array(

    'select'=>'title',

    'condition'=>'postID = :postID)',

    'params'=>array(':postID'=>$sIds),

)); ?>


<?php foreach($posts as $post) : ?>

<?php echo $post->title; ?><br/>

<?php endforeach; ?>



And this will work where postID IN (1):




<?php $sIds = '1';


$posts=Post::model()->findAll(array(

    'select'=>'title',

    'condition'=>'postID IN (:postID)',

    'params'=>array(':postID'=>$sIds),

)); ?>


<?php foreach($posts as $post) : ?>

<?php echo $post->title; ?><br/>

<?php endforeach; ?>



But this doesn’t work where postID IN (1,2):s:




<?php $sIds = '1,2';


$posts=Post::model()->findAll(array(

    'select'=>'title',

    'condition'=>'postID IN (:postID)',

    'params'=>array(':postID'=>$sIds),

)); ?>


<?php foreach($posts as $post) : ?>

<?php echo $post->title; ?><br/>

<?php endforeach; ?>



It will not echo anything, the two previous will.

[SOLVED] - you must not use ‘params’ just ‘condition’




<?php $sIds = '1,2';


$posts=Post::model()->findAll(array(

    'select'=>'title',

    'condition'=>'postID IN ('.$sIds.')',

)); ?>

<?php foreach($posts as $post) : ?>

<?php echo $post->title; ?><br/>

<?php endforeach; ?>



The above will nicely echo array elements. Thanks everyone for your input.

You could also use CDbCriteria::addInCondition(). But in your case you then could not supply the criteria properties in the call to find() but would have to create the criteria manually before.

EDIT: fixed typo

if you have an array of ids, you can use addInCondition:




$ids=array(1,2,3);

$criteria= new CDbCriteria;

$criteria->addInCondition('postID', $ids);

$posts=Post::model()->findAll($criteria);



Yeah zaccaria, your code works the same as mine:




<?php $sIds = '1,2';


$posts=Post::model()->findAll(array(

    'select'=>'title',

    'condition'=>'postID IN ('.$sIds.')',

)); ?>



Thanks all.