Geoffrey De Vylder
Geoffrey De Vylder

Reputation: 4143

doctrine 2 many to many relation

I'm creating my Doctrine 2 mappings (in XML). I would like to create a many-to-many relationship between the entities Snippet and Tag using:

Snippet:

<many-to-many field="tags" target-entity="Tag">
    <cascade><cascade-all /></cascade>
</many-to-many>

Tag

<many-to-many field="snippets" target-entity="Snippet">
    <cascade><cascade-all /></cascade>
</many-to-many>

However when I use the schematool to generate my database tables, two tables are added. tag_snippet and snippet_tag. Is there any way to use only 1 table instead of 2?

A solution would be only defining the relation in one of the entities but will I be able to access it from the other one? (because no specific property is defined the other way around)

Looks like I'm missing something obvious here.

Upvotes: 1

Views: 1687

Answers (1)

timdev
timdev

Reputation: 62864

Looks like you need to decide which is the "owning side" (in Doctrine parlance), and add the appropriate "mapped-by" and "inversed-by" attributes to your ManyToMany tags.

See the documenation, specifically the XML example.

Upvotes: 2

Related Questions