$criteria = new CDbCriteria;
$criteria->select = array('Category');
$criteria->condition = "IdCategory IN ($cats)";
$selection = Category::model()->findAll($criteria);
Page 1 of 1
find() to simple array
#1
Posted 20 January 2010 - 05:48 AM
hi there, is there a function to convert the output of the query below to a simple array (array('Cat1', 'Cat2', Cat3)).
#2
Posted 20 January 2010 - 05:54 AM
Don't think so, but where is your difference between
and the following?
foreach($cats as $cat) do_something_with($cat->Category);
and the following?
foreach($cats as $cat) do_something_with($cat);
Utinam ea res tibi usui fuerit!
#3
Posted 20 January 2010 - 05:55 AM
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
thank you
#4
Posted 20 January 2010 - 06:35 AM
$cats=array_map( create_function('$a','return $a[\'Category\'];'),$cats);
#6
Posted 20 January 2010 - 07:16 AM
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();
#7
Posted 20 January 2010 - 07:24 AM
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);
#8
Posted 20 January 2010 - 07:25 AM
it's getting better and better. but with the code below its only printing the first character of the categories
EDIT: debugging tells me that the array_map is not working correctly
EDIT 2: my bad dont need the array_map anymore
$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
#9
Posted 20 January 2010 - 07:32 AM
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.
Share this topic:
Page 1 of 1

Help













