开发者

Zend_DBTable relationships

I know that the question has certainly be asked several times, but I can't find (understandable) responses to it.

I've to tables 'spots' & 'members' and a relation table 'spotmembers'.

In the zend framework, i'm using Zend_Db_Table but I don't manage to retrieve objects from my relationship in 开发者_运维技巧my requests.

Here are my classes :

class Model_DbTable_Member extends Zend_Db_Table {
protected $_name = 'members';
protected $_primary = 'id';
protected $_dependentTables = array('Model_DbTable_SpotMember');

class Model_DbTable_SpotMember extends Zend_Db_Table {
protected $_name = 'spotmembers';
protected $_primary = 'id';
protected $_referenceMap = array(
    'Spot' => array(
        'columns' => 'id_spot',
        'refTableClass' => 'Model_DbTable_Spot',
        'refColumns' => 'id'
    ),
    'Member' => array(
        'columns' => 'id_member',
        'refTableClass' => 'Model_DbTable_Member',
        'refColumns' => 'id'
    )
);

I would like to make a request in order to retrieve Member objects related to a Spot object, any idea about how to do that ?

Regards.


You can try this for a simple relation OneToMany:

$Spot = new Model_DbTable_SpotMember();
$SpotDB = $Spot->find($id)->current();

$Members = $SpotDB->findParentModel_DbTable_Member();
Zend_Debug::dump($Members->toArray();

For a relation ManyToMany, somethink like that:

$Members = $Spot->findModel_DbTable_MemberViaModel_DbTable_SpotMember('MemberName','MemberInfo');
Zend_Debug::dump($Members->toArray();

All documentation, can be found here: http://framework.zend.com/manual/en/zend.db.table.relationships.html

Example #9:

$bugsTable = new Bugs(); // Your "Spot"
$bugsRowset = $bugsTable->find(1234); // Loading "Spot" and retreive RowSet
$bug1234 = $bugsRowset->current(); // Retrieve

$productsRowset = $bug1234->findManyToManyRowset('Products', // Your "Members" table
                                             'BugsProducts', // YOur "relation" table
                                             'Bug'); // The crurent table "Spot"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜