Reputation: 13340
I am starting to understand how Symfony 2 and Doctrine handle many to many relationships, but I am stuck on something, that I seem to have read differing opinions on.
Let's say I have an Author entity and a Book entity...with a many-to-many relationship between them.
In my database, I represent this with three tables, an authors
table, a books
table, and an authors_books
table (which basically just joins them together.
In Symfony 2, do I also create an AuthorsBooks
entity, which would have the sole purpose of joining the Author and Book entities together? Or, does Symfony handle that for me?
Upvotes: 0
Views: 1401
Reputation: 3336
If there is nothing else than the association of authors and books, you don't need to create the AuthorsBooks
entity.
You have two options now, Unidirectional or Bidirectional. You'll be able to find more information directly in Doctrine2 documentation here
For unidirectional, you'll have something like that (yml):
Author:
type: entity
manyToMany:
bookList:
targetEntity: Group
joinTable:
name: authors_books
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
book_id:
referencedColumnName: id
Upvotes: 2
Reputation: 44406
Symfony has nothing to do with this, it's strictly Doctrine's thing. But, no, you do not have to create a AuthorsBooks
entity. All you have to do is to configure Doctrine properly, i.e. use @JoinTable
annotation that specifies which table should be used to join two entities.
Upvotes: 1