2014-02-11 17 views
0

我發現這個迴應:https://stackoverflow.com/a/9135093/620095,但我不想使用一個新的類。我正在尋找最好的方式(優雅和直接)。如何在單向多對多關係中創建額外的域?

這裏是 '標籤' 實體:

MyApp\CoreBundle\Entity\Tag: 
    type: entity 
    table: tag 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     name: 
      type: string 
      length: 255 
      nullable: false 

'後' 實體:

MyApp\PostBundle\Entity\Post: 
    type: entity 
    table: post 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     title: 
      type: string 
      length: 255 
      nullable: false 
     content: 
      type: text 
      nullable: false 
    manyToMany: 
     Tags: 
      targetEntity: MyApp\CoreBundle\Entity\Tag 
      joinTable: 
       name: post_tag 
       joinColumns: 
        post_id: 
         referencedColumnName: id 
       inverseJoinColumns: 
        tag_id: 
         referencedColumnName: id 

的 '事件' 實體:

MyApp\EventBundle\Entity\Event: 
    type: entity 
    table: event 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     title: 
      type: string 
      length: 255 
      nullable: false 
     content: 
      type: text 
      nullable: false 
    manyToMany: 
     Tags: 
      targetEntity: MyApp\CoreBundle\Entity\Tag 
      joinTable: 
       name: event_tag 
       joinColumns: 
        event_id: 
         referencedColumnName: id 
       inverseJoinColumns: 
        tag_id: 
         referencedColumnName: id 

假設我要一個' 'event_tag'表中的'description'字段。我該怎麼辦?

更新:如果是不可能的,如何保持我的三個yml添加引用字段?

回答

0

對不起,您必須創建一個新實體。

你必須considere這個實體作爲一個單獨的symfony實體

例如:

Product >- Stock -< Store 
Product >- Bill -< User 
... 

並沒有像其他兩個實體之間的聯繫。

+0

我更新了問題。你可以幫我嗎? – cbacelar

+0

我懷疑沒有。這只是Doctrine 2的工作方式。我想你可以切換到不同的ORM,但我沒有任何支持你想要的。用兩個ManyToOne替換ManytoMany並不是什麼大不了的事情。 – Cerad

0

@cbacelar「我更新了這個問題,你能幫助我嗎?」 =>事件標籤的關係可以某物像:

在事件實體,改變多對多的一對多:

oneToMany: 
     EventTags: 
      targetEntity: MyApp\EventBundle\Entity\EventTag 
      mappedBy: Events 

在標籤實體,通過一對多改變多對多:

oneToMany: 
     EventTags: 
      targetEntity: MyApp\EventBundle\Entity\EventTag 
      mappedBy: Tags 

創建MyApp的\ CoreBundle \實體\ EventTag實體:

type: entity 
table: event_tag 
id: 
    id: 
     type: integer 
     generator: { strategy: AUTO } 
fields: 
    description: 
     type: text 
     nullable: false 
manyToOne: 
    Events: 
     targetEntity: MyApp\EventBundle\Entity\Event 
     inversedBy: EventTags 
     joinColumn: 
      name: event_id 
      referencedColumnName: id 
    Tags: 
     targetEntity: MyApp\CoreBundle\Entity\Tag 
     inversedBy: EventTags 
     joinColumn: 
      name: tag_id 
      referencedColumnName: id 

`

+0

好的......但是當我生成'事件'實體crud時,字段'標記'不會出現。如何嵌入表單? – cbacelar

+0

我還沒有做過那種形式的事情,但我可以給你幾個想法: 1)請參閱 http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation- with.html 2)嘗試創建一個未映射的字段與標籤實體(和查詢生成器,請參閱http://symfony.com/doc/current/reference/forms/types/entity.html),然後添加您的EventTag實體在前/後提交:http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html 後來,如果我有幾分鐘,我會考慮這一點,我會給你一個更好的答案。可能有更好的方法。 – jdharandas

+0

你找到你的答案了嗎?請檢查:http://symfony.com/doc/current/cookbook/form/form_collections.html – jdharandas