我目前正在使用 Symfony 2.1.0-DEV 和 Doctrine 2.2.x 使用Sonata Admin Bundle,我遇到了 Many-To的问题 - 许多实体关联:
class MyProduct extends Product {
/**
* @ORM\ManyToMany(targetEntity="Price")
*/
private $prices;
public function __construct() {
$this->prices = new \Doctrine\Common\Collections\ArrayCollection()
}
public function getPrices() {
return $this->prices;
}
public function setPrices($prices) {
$this->prices = $prices;
}
}
// Admin Class
class GenericAdmin extends Admin {
...
public function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('prices', 'sonata_type_model')
->end()
;
}
}
...
}
现在,如果尝试为Sonata的CRUD 创建/编辑表单面板中的多对多关联添加价格,则更新不起作用。
有关此问题的任何提示吗?谢谢!
答案 0 :(得分:8)
我找到了问题的答案:为了让事情与多对多关系一起使用,您需要传递* by_reference *等于 false (有关详细信息,请参阅here。
更新的工作版本是:
class MyProduct extends Product {
/**
* @ORM\ManyToMany(targetEntity="Price")
*/
private $prices;
public function __construct() {
$this->prices = new \Doctrine\Common\Collections\ArrayCollection()
}
public function getPrices() {
return $this->prices;
}
public function setPrices($prices) {
$this->prices = $prices;
}
public function addPrice($price) {
$this->prices[]= $price;
}
public function removePrice($price) {
$this->prices->removeElement($price);
}
}
// Admin Class
class GenericAdmin extends Admin {
...
public function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('prices', 'sonata_type_model', array('by_reference' => false))
->end()
;
}
}
...
}