I’m currently trying to map music related data using Doctrine2’s POPO Annotations.
I haven’t had problems mapping any other many-to-many relations, but one specific relation is giving me trouble. It does not throw an error, but the mapping does not get inserted into the mapping table (artist_album)
Artist:
<?php
/**
* @orm:Entity
* @orm:Table(name="artist")
*/
class Artist
{
...
/**
* @orm:ManyToMany(targetEntity="Company\MusicBundle\Entity\Album", inversedBy="artists", cascade={"persist"})
* @orm:JoinTable(name="artist_album",
* joinColumns={@orm:JoinColumn(name="artist_id", referencedColumnName="id")},
* inverseJoinColumns={@orm:JoinColumn(name="album_id", referencedColumnName="id")})
*
* @var ArrayCollection
*/
private $albums;
...
}
Album
....
/**
* @orm:ManyToMany(targetEntity="Company\MusicBundle\Entity\Artist", mappedBy="albums", cascade={"persist"})
*
* @var ArrayCollection
*/
private $artists;
...
}
I’m sure it’s just something in I’ve done wrong in the mapping, but I just can’t put my proverbial finger on it.
My problem was that I was setting the artist on the inverse side of the relationship. It appears you must set the relationship on the owning side (in this case, Artist).