簡體   English   中英

Sonata Admin Bundle不使用多對多實體關系

[英]Sonata Admin Bundle not working with Many-to-Many entity relationships

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

    ...

}

現在,如果嘗試為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()
            ;
        }
    }

    ...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM