Sonata 管理捆绑包不适用于多对多实体关系


Sonata Admin Bundle not working with Many-to-Many entity relationships

我目前正在使用使用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()
            ;
        }
    }
    ...
}

现在,如果尝试从Sonata的CRUD创建/编辑表单面板向多对多关联添加价格,则更新不起作用。

关于这个问题有什么提示吗?谢谢!

使用解决方案进行更新

我已经找到了我的问题的答案:为了使事情与多对多关系一起工作,你需要传递 *by_reference* 等于 false(有关更多详细信息,请参阅此处)。

更新的工作版本是:

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()
            ;
        }
    }
    ...
}