有几件事你必须正确设置才能使其工作。
首先,你需要告诉学说$versions
有关Version
:
class Shop
{
private $id;
private $url;
/**
* @ORM\ManyToMany(targetEntity="Version", cascade={"persist"})
* @ORM\JoinTable(name="shop_version",
* joinColumns={@ORM\JoinColumn(name="shop_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")}
* )
*/
private $versions;
}
因为它是一个ManyToMany
关系(documentation),$versions
会像ArrayCollection
由Symfony的处理。因此,您需要创建相应的方法来处理它。
构造
public function __construct()
{
$this->versions = new ArrayCollection();
}
消气
public function getVersions()
{
return $this->versions;
}
加法
public function addVersion(Version $version)
{
$this->versions[] = $version;
}
卸妆
public function removeVersion(Version $version)
{
$this->versions->removeElement($version);
}
就是这样。不要忘记为ArrayCollection
添加use
陈述!
use Doctrine\Common\Collections\ArrayCollection;
谢谢你的提示。 – Roman 2014-09-06 07:51:55