homelessDevOps
homelessDevOps

Reputation: 20726

Zend_Db_Select and N:N Relations

I have som trouble with Zend_Db_Select, its a long time ago since i have used it (-: I need an result array like this:

USER 0
 ID -> 1
 MY_NUMBERS -> (array)
    -> 1 ONE
    -> 2 TWO

USER 1 (array)
 ID -> 2 (int)
 MY_NUMBERS -> (array)
    -> 1 ONE
    -> 2 TWO
    -> 3 THREE
    -> 10 TEN

This a part of my Query

$select->from(array('USERS' => $table))
    ->join(
        array('USERS_NUMBERS' => 'USERS_HAS_NUMBERS'), // many to many
        'USERS.ID = USERS_NUMBERS.USER_ID'
    )
   ->joinLeft(
        array('N' => 'NUMBERS'),
        'USERS_NUMBERS.NUMBER_ID = NUMBERS.ID', 
        array('MY_NUMBERS' => 'NUMBERS.NUMBER')
   );

But in the moment i get one record returned for each existing relationship, so if user 1 hast 5 Numbers i get 5 full records returned for this user.

I know that my select is wrong in the moment to get the array like i need it, but i have to idea how to do :-)

Upvotes: 1

Views: 38

Answers (1)

Tobias
Tobias

Reputation: 1682

I would use Zend_Db_Table and set up the relations in there, so your code is also more readable. http://framework.zend.com/manual/1.12/en/zend.db.table.relationships.html

Then when you have your user you can do something like:

$user->findDependentRowset('numbers');

Upvotes: 1

Related Questions