0

如何从“许多”表记录中获取对象?

我有列表,rsObjectComments我需要获取rsObjects.

例如:

架构.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 } 
4

1 回答 1

0

我鼓励您阅读整个文档页面,名为:Inside The Model Layer (Doctrine)(特别是检索相关记录部分)。

您将看到一个带有许多评论的文章的基本示例(与您的几乎相同)。

对于您的示例,如果您有一个,rsObject您可以使用以下方法获取相关rsObjectComments信息:

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

我正在使用getComments,因为您定义了foreignAliaswith Comments

如果你想在他们身上工作

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

并且以相反的顺序,如果您有评论并想要检索其文章。

$rsObject = $comment->getRsObject();
于 2012-08-29T08:52:00.737 回答