2012-08-29 81 views
0

如何從「多」表記錄中獲取對象?symfony 1.4:如何從關係表中獲取記錄

我有rsObjectComments的列表,我需要取rsObjects

例如:

的schema.yml:

rsObject: 
    actAs: 
    Timestampable: ~ 
    Sluggable: 
     fields: [name] 
    columns: 
    name: { type: string(255), notnull: true, unique: true } 
    description: { type: string(6000), notnull: true } 
    relations: 
    rsObjectComments: 
    class:  rsObjectComments 
    local:  id 
    foreign:  rsobject_id 
    type:   many 
    foreignAlias: Comments 

rsObjectComments: 
    actAs: 
    Timestampable: ~ 
    columns: 
    rsobject_id: { type: integer, notnull: true } 
    fromname: { type: string(100), notnull:true } 
    fromemail: { type: string(100), notnull:true } 
    comments: { type: string(1000), notnull:true }  
    is_public: { type: boolean, notnull: true, default: 1 } 
    relations: 
    rsObject: { onDelete: CASCADE, local: rsobject_id, foreign: id, foreignAlias: rsObjectCommentsAlias } 

回答

0

我建議你閱讀全稱爲文檔頁面:Inside The Model Layer (Doctrine)(特別是部分檢索相關記錄)。

您將看到一篇文章的基本示例,其中包含許多評論(與您的評論幾乎相同)。

對於你的榜樣,如果你有一個rsObject您可以獲取使用相關rsObjectComments:因爲你Comments定義的foreignAlias

// will return a Doctrine_Collection 
$rsObjectComments = $rsObject->getComments(); 

我使用getComments

如果你想對他們

foreach ($rsObject->getComments() as $comment) 
{ 
    // $comment is a rsObjectComments object 
} 

而在相反的順序工作,如果您有任何意見和希望檢索其文章。

$rsObject = $comment->getRsObject();