带有连接表的Doctrine 2 ManyToOne [英] Doctrine 2 ManyToOne with Join Table

查看:92
本文介绍了带有连接表的Doctrine 2 ManyToOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关如何映射使用联接表的OneToMany / ManyToOne关系的建议。我没有进行映射,并且收到错误消息,未在媒体表中设置article_id。

I'm looking for a suggestion on how to map a OneToMany/ManyToOne relationship that uses a join table. The mapping I have is not taking, and I get an error that article_id is not set in the media table. 

class Media
{
    // ...

    /**
     * @ManyToOne(targetEntity="Document", inversedBy="media")
     * @JoinTable(name="articles_x_media", referencedColumnName="id")
     * joinColumns={@JoinColumn(name="media_id", referencedColumnName="id")},
     * inverseJoinColumns={@JoinColumn(name="bid_id", referencedColumnName="id")})
     * )
     */
    protected $document;
}

class Document
{
    // ...

    /**
     * @OneToMany(targetEntity="Media", mappedBy="document"))
     * @JoinTable(name="articles_x_media", referencedColumnName="id")
     * joinColumns={@JoinColumn(name="article_id", referencedColumnName="id")},
     * inverseJoinColumns={@JoinColumn(name="media_id", referencedColumnName="id")}
     * )
     */
    protected $media;
}


推荐答案

关于 OneToMany 与联接表的映射

There's a specific paragraph in the documentation about OneToMany mapping with join table.

无论如何,您可能想要的是单向 ManyToMany 关联

Anyway, what you probably want is an uni-directional ManyToMany association.

此外, @OneToMany 不带有 @JoinTable ,对于 @ManyToOne 也是如此。

Also, @OneToMany does not come with a @JoinTable, and the same for @ManyToOne either.

这篇关于带有连接表的Doctrine 2 ManyToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆