【问题标题】:Sonata Admin Bundle not working with Many-to-Many entity relationshipsSonata Admin Bundle 不适用于多对多实体关系
【发布时间】:2012-07-05 17:05:55
【问题描述】:

我目前正在使用使用 Symfony 2.1.0-DEVDoctrine 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 create/edit 表单面板向多对多关联添加价格,则更新不起作用。

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

【问题讨论】:

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


    【解决方案1】:

    更新解决方案

    我找到了我的问题的答案:为了使事情能够处理 多对多 关系,您需要传递 *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()
                ;
            }
        }
    
        ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 2020-04-10
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2016-09-03
      • 2016-04-24
      • 2018-02-01
      相关资源
      最近更新 更多