[英]Sonata Admin Bundle not working with Many-to-Many entity relationships
i'm currently using Sonata Admin Bundle using Symfony 2.1.0-DEV and Doctrine 2.2.x and i'm having problems with Many-To-Many entities associations : 我目前正在使用Symfony 2.1.0-DEV和Doctrine 2.2.x使用Sonata Admin Bundle,而且我遇到了多对多实体关联的问题 :
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()
;
}
}
...
}
Now if try to add a price to the many-to many association from Sonata's CRUD create/edit form panel the update doesn't work. 现在,如果尝试为Sonata的CRUD 创建/编辑表单面板中的多对多关联添加价格,则更新不起作用。
Any hints on this issue? 关于这个问题的任何提示? Thanks! 谢谢!
I've found the answer to my problem: for making things to work with many-to-many relationships you need to pass *by_reference* equal to false (see here for more details). 我已经找到了我的问题的答案:为了让事情与多对多关系一起工作,你需要传递* by_reference *等于false (有关详细信息,请参阅此处 )。
The updated working version is: 更新的工作版本是:
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()
;
}
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.