douyi2798 2016-01-17 14:08
浏览 96
已采纳

如何从JoinTable映射到Doctrine

I have the following tables in my ERM/database:

card, expansion and card_expansion
ManyToMany-Relationship between card and expansion. A card can be in many expansions and an expansion can hold a many of cards.

collection
Description of how many cards from which expansion are needed. Card A from Expansion B is needed 3 times. So there's a OneToMany relationship to card_expansion.

I want to map these relations in my Entities-objects using Doctrine. Mapping between cards and expansions is no problem. But since the card_expansion table isn't mapped directly by an entity, I don't have a clue how to access it from my Collection entity or which annotation I have to use to build up that connection correctly...

What do I need to map to get this working?

Here's the code of my Expansion entity:

/**
 * @Entity
 * @Table(name="expansion")
 */
class Expansion {

...
    /**
     * @ManyToMany(targetEntity="Card")
     * @JoinTable(name="card_expansion",
     *     joinColumns={@JoinColumn(name="_expansion", referencedColumnName="id")},
     *     inverseJoinColumns={@JoinColumn(name="_card", referencedColumnName="id", unique=true)})
     */
    private $cards;

    public function __construct () {
        $this->cards = new ArrayCollection();

    }

and here's my Collection Entity

/**
 * @Entity
 * @Table(name="collection")
 */
class Collection{

 /**
     * @OneToMany(targetEntity="???")
     * @JoinTable(name="card_in_expansion")
     */
    private $card_in_expansion;
  • 写回答

1条回答 默认 最新

  • dongtaidai0492 2016-01-18 09:09
    关注

    You can make the relationship between Card and Expension to an entity CardExpension. Then you will be able to use it in your model.

    Instead of having following:

    Card - ManyToMany - Expension

    You will get:

    Card - OneToMany - CardExpension - ManyToOne - Expension

    Read also on this here in the Doctrine 2 documentation:

    Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站