bas_vdl
(Bas Vdl)
1
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);
Daniel
(Info)
2
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);
bas_vdl
(Bas Vdl)
3
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
mikl
(Mike)
4
$cats=array_map( create_function('$a','return $a[\'Category\'];'),$cats);
mikl
(Mike)
6
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();
bas_vdl
(Bas Vdl)
7
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);
bas_vdl
(Bas Vdl)
8
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 
mikl
(Mike)
9
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.
jonah
(Poppitypop)
10