简体   繁体   English

Zend_Db_Select和N:N关系

[英]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: 我在使用Zend_Db_Select时遇到了麻烦,因为我已经使用了它(-:我需要这样的结果数组:

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. 但是目前我为每个现有关系返回了一条记录,所以如果用户1有5个数字,我将为该用户返回5条完整记录。

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 :-) 我知道我的选择在获得我需要的数组的那一刻是错误的,但我必须知道如何做:-)

I would use Zend_Db_Table and set up the relations in there, so your code is also more readable. 我将使用Zend_Db_Table并在那里建立关系,因此您的代码也更具可读性。 http://framework.zend.com/manual/1.12/en/zend.db.table.relationships.html http://framework.zend.com/manual/1.12/zh/zend.db.table.relationships.html

Then when you have your user you can do something like: 然后,当您拥有用户时,您可以执行以下操作:

$user->findDependentRowset('numbers');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM