简体   繁体   English

重复关系中的一对多关系(表中的第三个键)

[英]How to many to many with repeated relationships (third key in table)

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? 在Doctrine2中,有可能使用第三个键来管理多对多关系,从而能够添加多个同一个关系?

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. 我有一个“用户”表和另一个“计划”表,并且我进行了正常的多对多关系来生成带有两个主键(user_id和plan_id)的user_plan表,但是我需要在我的应用程序中能够添加相同的表向用户计划一次以上。 For example: user_plan(generated_id, user_id, plan_id) 例如:user_plan(generated_id,user_id,plan_id)

My current user yml definition: 我当前的用户yml定义:

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: 我目前的计划yml定义:

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? 有人知道用symfony2可以做到吗?

I don't know about third key, but i see another solution. 我不知道第三把钥匙,但是我看到了另一种解决方案。 You can add another model PlantBed. 您可以添加另一个模型PlantBed。 User has_many PlantBeds (PlantBed has_one User). 用户has_many PlantBeds(PlantBed has_one用户)。 PlantBed has_one Plant (Plant has_many PlantBeds) and quantityOfPlantsInBed. PlantBed has_one植物(Plant has_many PlantBeds)和quantityOfPlantsInBed。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM