pc131
(Skoczy)
November 18, 2010, 9:37pm
1
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?
awesomejuice
(C Samual Davidson)
November 18, 2010, 9:43pm
2
Hi, try:
$sIds = '1,2,3,4';
$post=Post::model()->find(array(
'select'=>'title',
'condition'=>'postID IN (:postID)',
'params'=>array(':postID'=>$sIds),
));
pc131
(Skoczy)
November 18, 2010, 10:08pm
3
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.
jacmoe
(Jacob Moen)
November 18, 2010, 10:12pm
4
<?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; ?>
pc131
(Skoczy)
November 18, 2010, 10:28pm
5
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?
tri
(tri - Tommy Riboe)
November 18, 2010, 10:39pm
6
pc131:
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
awesomejuice
(C Samual Davidson)
November 18, 2010, 10:42pm
7
Yes actually it should be [url="http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail" ]findAll/url if you want to get an array of multiple posts.
pc131
(Skoczy)
November 18, 2010, 10:49pm
8
Ok
After I used findAll print_r($posts) now renders “Array()” in html, what means that this findAll haven’t returned any values?
pc131
(Skoczy)
November 18, 2010, 11:14pm
9
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.
mikl
(Mike)
November 19, 2010, 7:55am
10
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
zaccaria
(Matteo Falsitta)
November 19, 2010, 7:57am
11
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);
pc131
(Skoczy)
November 19, 2010, 8:52am
12
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.