我有这两个类与ManyToMany关联:
一堂课:
/**
* @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="tripEvents")
* @ORM\JoinTable(name="event_trip_registrators")
*/
private $tripRegistrators;
public function __construct()
{
$this->tripRegistrators = new ArrayCollection();
}
public function getTripRegistrators()
{
return $this->tripRegistrators;
}
public function setTripRegistrators($tripRegistrators)
{
$this->tripRegistrators = $tripRegistrators;
}
public function addTripRegistrator(User $tripRegistrator)
{
$this->tripRegistrators->add($tripRegistrator);
}
public function removeTripRegistrator($tripRegistrator)
{
$this->tripRegistrators->removeElement($tripRegistrator);
}
第二课:
/**
* @ORM\ManyToMany(targetEntity="Bundle\Entity\Event", mappedBy="tripRegistrators")
*/
protected $tripEvents;
public function __construct()
{
parent::__construct();
$this->tripEvents = new ArrayCollection();
}
public function getTripEvents()
{
return $this->tripEvents;
}
public function setTripEvents($tripEvents)
{
$this->tripEvents = $tripEvents;
}
如果我调用$event->getTripRegistrators()
(第一类),我只会得到一个空的持久集合。
你有任何暗示为什么会这样吗?
如果我通过SonataAdmin保存项目,一切正常,数据库表有正确的数据。