【问题标题】:How to many to many with repeated relationships (third key in table)具有重复关系的多对多(表中的第三个键)
【发布时间】:2012-12-11 17:57:50
【问题描述】:

在 Doctrine2 中可以使用第三个键来管理多对多关系,以便能够添加多个相同的关系?

我有一个“用户”表和另一个“计划”表,我做了正常的多对多关系,生成带有两个主键(user_id 和 plan_id)的 user_plan 表,但我需要在我的应用程序中能够多次向用户添加相同的计划。例如:user_plan(generated_id, user_id, plan_id)

我当前的用户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: []

我目前的计划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 ]

有人知道symfony2是否可以做到这一点?

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

    我不知道第三把钥匙,但我看到了另一种解决方案。您可以添加另一个模型 PlantBed。用户 has_many PlantBeds(PlantBed has_one 用户)。 PlantBed has_one Plant (Plant has_many PlantBeds) 和数量OfPlantsInBed。

    【讨论】:

    猜你喜欢
    • 2019-12-29
    • 2016-07-22
    • 2020-02-04
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多