I have three tables:
- game
columns: id | name(eg. 1 | Halo) - own
columns: id | type_id | type_name(eg. 1 | 1 | on wishlist) - user
columns: id | name(eg. 1 | Tomek)
Now what I have is a ManyToMany connection between those tables:
User (table user_own)
/**
* @ORM\ManyToMany(targetEntity="Own")
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="type_id", referencedColumnName="id")}
* )
*/
Own (table own_game)
/**
* @ORM\ManyToMany(targetEntity="Game")
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")}
* )
*/
My query:
public function getOwned() {
return $this->getEntityManager()
->createQuery('
SELECT u, o, g FROM AcmeGoBundle:User u
JOIN u.owners o
JOIN u.games g
')
->getResult();
}
I can retrieve games owned by particular user, but I can’t connect type of ownership. How can I do this?
To crate
ManyToManywith additional column with some data you need to create this by hand.Doctrine
ManyToManyrelation crates a join table with id’s of joined entities. So you need to create by your own Entity named ex.UserOwnswithOneToManyrelation toUserand anotherOneToManyrelationOwn. Then you can add additional field with your data inside.Check that question: Doctrine2: Best way to handle many-to-many with extra columns in reference table