0

It's possible in Doctrine2 to manage Many to Many relations with a third key to be able to add more than one identic relationship?

I have one "Users" table and one other "Plans" table and i did the normal many to many relationship that produces user_plan table with the two primary keys (user_id and plan_id) but I need in my application to be able to add the same plan to user more than one time. For example: user_plan(generated_id, user_id, plan_id)

My current user yml definition:

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: []

My current plan yml definition:

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 ]

Someone knows if it's possible to do that with symfony2?

Genar
  • 433
  • 3
  • 22

1 Answers1

0

I don't know about third key, but i see another solution. You can add another model PlantBed. User has_many PlantBeds (PlantBed has_one User). PlantBed has_one Plant (Plant has_many PlantBeds) and quantityOfPlantsInBed.

  • I found the answer in other post: http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table – Genar Dec 14 '12 at 15:13