Yii Framework Forum: find() to simple array - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

find() to simple array Rate Topic: -----

#1 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

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)).

$criteria = new CDbCriteria;
$criteria->select = array('Category');
$criteria->condition = "IdCategory IN ($cats)";
$selection = Category::model()->findAll($criteria);

0

#2 User is offline   Daniel M 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 19-January 10
  • Location:Germany, Saarland

Posted 20 January 2010 - 05:54 AM

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);

Utinam ea res tibi usui fuerit!
0

#3 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

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
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 20 January 2010 - 06:35 AM

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

0

#5 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

Posted 20 January 2010 - 06:46 AM

nice one! thank you.
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

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();

0

#7 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

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);

0

#8 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

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

                $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 :(
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

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.
0

#10 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 21 January 2010 - 03:34 PM

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

http://www.yiiframew...listData-detail
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users