Get One Result Per Related Result From Findall

Hi everyone,

I wonder if it is possible, to stop Yii from putting all related models returned from a query into the relation-attribute of the main model, and have one main model per returned related model instead.

The following minimal example illustrates my problem:

Consider two tables: numbers and letters.

Numbers has the field number with entries 1,2

Letters has the field letter with entries ‘a’, ‘b’, ‘c’ (and many more).

There is a many_many relationship between numbers and letters. The pivot table gives the following affiliations:

1 -> 'a'

2 -> 'b'

1 -> 'c'

What i need is an ordered list of letters that are affiliated with numbers, with the numbers they are related to.

Right now I create a criteria with

$criteria->order = 'letters.letter'

and then do

$numbers = Numbers::model()->with(letters)->findAll($criteria);

From this yii creates a query that will return the data I need when I run it by hand:

numbers letters

1 a

2 b

1 c

However, yii will put the model containing the letter c into $numbers[0]->letters[1], and I loose the ordering.

Is there any way to get three results from the findAll like so:

$numbers[0]->number = 1; numbers[0]->letters[0] = 'a'

$numbers[1]->number = 1; numbers[1]->letters[0] = 'b'

$numbers[2]->number = 1; numbers[2]->letters[0] = 'c'

Or maybe a way to get the array data returned from the query?

My nails curl up when I think of recreating the order of the letters by php sort functions, when all the information was already returned from the database.

Thank you for reading this. Any help would be greatly appreciated.

foreach($numbers as $data)


 $data1 = $data['name_of_field'];


Thank you for your reply. I dont see how this could work though, as count($numbers) = 2, and I need 3 lines.