Sonata Admin Bundle不使用多对多实体关系

Gia*_*lli 3 php doctrine-orm symfony-sonata symfony-2.1

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

    ...

}
Run Code Online (Sandbox Code Playgroud)

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

关于这个问题的任何提示?谢谢!

Gia*_*lli 8

用解决方案更新

我已经找到了我的问题的答案:为了让事情与多对多关系一起工作,你需要传递*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()
            ;
        }
    }

    ...

}
Run Code Online (Sandbox Code Playgroud)