【问题标题】:symfony 1.4: How to fetch records from relation tablesymfony 1.4:如何从关系表中获取记录
【发布时间】:2012-08-23 18:51:16
【问题描述】:

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

我有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 } 

【问题讨论】:

    标签: orm symfony1 doctrine symfony-1.4


    【解决方案1】:

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

    您将看到一个包含许多 cmets 的文章的基本示例(与您的几乎相同)。

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

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

    我使用getComments 是因为您使用Comments 定义了foreignAlias

    如果你想处理它们

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

    如果您有评论并想检索其文章,则顺序相反。

    $rsObject = $comment->getRsObject();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多