Yii Framework Forum: Ability to hash findAll result set - Yii Framework Forum

Jump to content

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

Ability to hash findAll result set Rate Topic: -----

#1 User is offline   Lilt 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 06-July 10

Posted 12 October 2010 - 11:59 AM

What do people think of the idea of adding the option to return an associative array, indexed by one of the fields of the data (generally a unique field), instead of a plain numerically indexed array for findAll and *_MANY relationships? I'm not sure if it's common to structure data sets like this... I find it useful to index by Primary Key, particularly when a lot of different related sets of data are being used. I'm unsure of the performance compares, using an associative array versus performing a foreach every time it is required, or performing an extra database lookup using a relation.

The conversion is trivial, I know, but given that the entire result set is being looped through anyway to create the array in the first place I thought the core might be a good place to add it.
1

#2 User is offline   Mike 

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

Posted 13 October 2010 - 02:23 AM

I still like this idea and find this feature very useful for different cases. Actually this already was requested several times but never implemented. I think there where concerns that it breaks some functionality (was it CDataProvider...? Can't remember.) Another concern was, that the chosen attribute might not be unique.

In my opinion, the developer will know, in which situation to use this and when not.

We could have a index property in CDbCriteria, that defaults to null. If set to an attribute name, this attribute is used to index the resultset.


So +1 from me.
1

#3 User is offline   CGeorge 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 27-May 10
  • Location:Spain

Posted 13 October 2010 - 08:39 AM

+1 from me too
0

#4 User is offline   Mike 

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

Posted 13 October 2010 - 10:18 AM

Finally opened a ticket now:
http://code.google.c.../detail?id=1667
0

#5 User is offline   Mike 

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

Posted 13 October 2010 - 10:28 AM

Here an example what this is useful for. Think of a small tree, stored in DB with parent/child relations. If i want to read in the complete tree and access every node and its children, i could now do this:

$categories=Category::model()->findAll(array('index'=>'id'));

foreach($categories as $id => $category)
  $categories[$category->parent_id]->children[$id] = $category;


Now i have a nice representation of the complete tree. Each node is accessible by its id. And each node has an array of its children.

If i want to do that now, i need another loop first to convert the zero based array into an id-indexed one.
0

#6 User is offline   Diane 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 08-February 12

Posted 08 February 2012 - 11:46 AM

What about fetching associative arrays with queryAll()?
Here is my code that I would like to return an associative array for each row in result set.

$sql = "SELECT l.* FROM tbl_listings l INNER JOIN tbl_paymentsource p
ON l.CustomerID = p.CustomerID WHERE p.Active = 1 and l.Active = 1";

$connection = Yii::app()->db;
$command = $connection->createCommand($sql);
$dataReader = $command->queryAll();
0

#7 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,375
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 08 February 2012 - 03:05 PM

View PostDiane, on 08 February 2012 - 11:46 AM, said:

What about fetching associative arrays with queryAll()?
Here is my code that I would like to return an associative array for each row in result set.

$sql = "SELECT l.* FROM tbl_listings l INNER JOIN tbl_paymentsource p
ON l.CustomerID = p.CustomerID WHERE p.Active = 1 and l.Active = 1";

$connection = Yii::app()->db;
$command = $connection->createCommand($sql);
$dataReader = $command->queryAll();

Won't work :(
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