Yii Framework Forum: findall() doesn't return a associative array - Yii Framework Forum

Jump to content

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

findall() doesn't return a associative array Rate Topic: -----

#1 User is offline   struvusmaximus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 11-October 09
  • Location:Coevorden, The Netherlands

Posted 11 October 2009 - 02:34 PM

I'am just trying the Yii framework and i have the following "problem".

i have a Auction model and when i want to get all the records in the mysql database using:
$auctions = Auction::model()->findAll();

I get a array with lots of data (CActiveRecordMetaData Object,CMysqlColumnSchema Objec etc.) and somewhere in this nested array structure is my data.

How do i get this data as a associative array?
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 11 October 2009 - 02:51 PM

findAll() returns you an array of instances of Auction (ActiveRecord).
You can access the properties/columns of each Auction model by (for example):

foreach ($auctions as $auction){
   $someProperty = $auction->property;
}


And set properties by:
$auction->property = $someProperty;

Don't say what you think, think what you say
The problem is communication! Excess of communication!
1

#3 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 11 October 2009 - 02:52 PM

May be you want to re-read this: ActiveRecord
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#4 User is offline   struvusmaximus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 11-October 09
  • Location:Coevorden, The Netherlands

Posted 11 October 2009 - 03:01 PM

Tnx, i made a foreach loop that calls getattributes on the objects and fills a array.

I was just hoping for some sort of convenient toArray() method but i couldn't find it.
0

#5 User is offline   SvenWin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 11-October 09
  • Location:Weiden, Germany

Posted 11 October 2009 - 03:54 PM

struvusmaximus: You should have a look at http://php.oregonsta...arrayaccess.php and http://www.php.net/~...rrayAccess.html . That's the technical background for above mentioned stuff.
0

#6 User is offline   szako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 17-May 10

Posted 22 December 2010 - 08:07 AM

View Poststruvusmaximus, on 11 October 2009 - 03:01 PM, said:

Tnx, i made a foreach loop that calls getattributes on the objects and fills a array.

I was just hoping for some sort of convenient toArray() method but i couldn't find it.


I understand what you wanted. I needed something like in_array() on a resultset. (Only one column's values were needed)

$attributeCodes = CHtml::listData(Attribute::model()->findAll(array('select' => 'code, id')), 'id', 'code');


This gave me the associated array.

The solution is also written here.
-------------
m(o_O)m
0

#7 User is offline   Mike 

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

Posted 23 December 2010 - 04:38 AM

Since Yii version 1.1.5 you can use the index property of CdbCriteria to get your result records indexed by any column you like.
1

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