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

时间:2012-07-05 09:52:25

标签: php doctrine-orm symfony-sonata symfony-2.1

我目前正在使用 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 创建/编辑表单面板中的多对多关联添加价格,则更新不起作用。

有关此问题的任何提示吗?谢谢!

1 个答案:

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

    ...

}