Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 ManyToOne with Join Table

Tags:

doctrine-orm

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;
}
like image 349
Shroder Avatar asked Mar 14 '13 18:03

Shroder


1 Answers

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

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

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

like image 122
Ocramius Avatar answered Jan 03 '23 01:01

Ocramius