2012-12-11 37 views
0

在Doctrine2中可以使用第三個關鍵字來管理多對多關係,以便能夠添加多個關係?如何多對多重複關係(表格中的第三個關鍵字)

我有一個「用戶」表和一個其他「計劃」表,我做了正常的多對多關係,產生user_plan表與兩個主鍵(user_id和plan_id),但我需要在我的應用程序能夠多次向用戶添加相同的計劃。例如:user_plan(generated_id,USER_ID,plan_id的數據類型)

我的當前用戶陽明定義:

Entity\FosUser: 
type: entity 
table: fos_user 
fields: 
    id: 
     id: true 
     type: integer 
     unsigned: false 
     nullable: false 
     generator: 
      strategy: IDENTITY 
manyToMany: 
    plans: 
     targetEntity: Plan 
     inversedBy: users 
     joinTable: 
      name: user_plan 
      joinColumns: 
       plan_id: 
        referencedColumnName: id 
      inverseJoinColumns: 
       user_id: 
        referencedColumnName: id 

lifecycleCallbacks: 
    prePersist: [ setUserValue ] 
    preUpdate: [] 

我目前的計劃陽明定義:

Entity\Plan: 
type: entity 
table: plan 
fields: 
    id: 
     id: true 
     type: integer 
     unsigned: false 
     nullable: false 
     generator: 
      strategy: IDENTITY 
    planName: 
     type: string 
     length: 50 
     fixed: false 
     nullable: false 
     column: plan_name 
manyToMany: 
    users: 
     targetEntity: FosUser 
     mappedBy: plans 
LifecycleCallbacks: 
    prePersist: [ setCreatedAtValue ] 
    preUpdate: [ setUpdatedAtValue ] 

有人知道是否有可能做到這一點與Symfony2的?

回答

0

我不知道第三個關鍵,但我看到另一個解決方案。您可以添加另一個模型PlantBed。用戶has_many PlantBeds(PlantBed has_one User)。 PlantBed has_one Plant(Plant has_many PlantBeds)and quantityOfPlantsInBed。

+0

我在其他帖子中找到了答案:http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table – Genar