find() to simple array

hi there, is there a function to convert the output of the query below to a simple array (array(‘Cat1’, ‘Cat2’, Cat3)).




$criteria = new CDbCriteria;

$criteria->select = array('Category');

$criteria->condition = "IdCategory IN ($cats)";

$selection = Category::model()->findAll($criteria);



Don’t think so, but where is your difference between


foreach($cats as $cat) do_something_with($cat->Category);

and the following?


foreach($cats as $cat) do_something_with($cat);

i thought maybe there is a function in Yii who will do that for me. but this is a good way to go.

thank you


$cats=array_map( create_function('$a','return $a[\'Category\'];'),$cats);

nice one! thank you.

Oh, and performance whise, i’d of course not use AR at all in this case:


$categories=Yii::app()->db->createCommand("SELECT Category FROM categories WHERE IdCategory IN($cats)")->queryColumn();

it’s getting better and better. but with the code below its only printing the first character of the categories




                $cats = implode(",", $_POST['Categories']);

                $categories = $categories=Yii::app()->db->createCommand("SELECT Category FROM Categories WHERE IdCategory IN($cats)")->queryColumn();

                $selection = array_map( create_function('$a','return $a[\'Category\'];'), $categories);

                $selection = implode(", ", $selection);



it’s getting better and better. but with the code below its only printing the first character of the categories




                $cats = implode(",", $_POST['Categories']);

                $categories = Yii::app()->db->createCommand("SELECT Category FROM Categories WHERE IdCategory IN($cats)")->queryColumn();

                $selection = array_map( create_function('$a','return $a[\'Category\'];'), $categories);

                $selection = implode(", ", $selection);



EDIT: debugging tells me that the array_map is not working correctly

EDIT 2: my bad dont need the array_map anymore :(

You don’t need array_map() with the queryColumn() variant. It already returns an array. See the other query* methods in CDbCommand for more options.

this is meant for drop-downs but it should work:

http://www.yiiframework.com/doc/api/CHtml#listData-detail