2016-05-16 54 views
0

我的實體「produit」與此關係:Symfony的persit收集

/** 
* @var \stdClass 
* 
* @ORM\ManyToMany(targetEntity="ListeBundle\Entity\GarDegatEaux", cascade={"persist"}) 
* @ORM\JoinTable(name="produit_garDegatEaux", 
*  joinColumns={@ORM\JoinColumn(name="produit_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="garDegatEaux_id", referencedColumnName="id", unique=false)}) 
*/ 
private $garDegatEaux; 

一種形式類型象下面這樣:

->add('garDegatEaux', CollectionType::class, array('entry_type' => GarDegatEauxType::class, 
                  'allow_add' => true, 
                  'allow_delete' => true, 
                  //'by_reference' => false, 
                  'prototype' => true, 
                  'label' => 'Coefficients dégât des eaux', 
                  'entry_options' => array('label' => 'Coefficients'), 
                  'attr' => array('class' => 'collection') 
                 )) 

當通過參考被設置爲false我有這樣的錯誤:

無論是屬性 「garDegatEaux」,也不的方法的一個 「addGarDegatEau()」/ 「removeGarDegatEau()」, 「setGarDegatEaux()」, 「garDegatEaux()」,「__set( )「或」__call()「存在並且在」DevisBundle \ Entity \ Produit「類中具有公共訪問權限。 by_reference 「:

在我的實體addGarDegatEau() 」/「 removeGarDegatEau()」。

$this->garDegatEaux = new \Doctrine\Common\Collections\ArrayCollection(); 

當並獲得..存在,有公共存取權限我也有我的構造函數與」

當然設置爲true沒有錯誤,但沒有任何提交的收集。當我轉儲表單之前的持久存在沒有任何ArrayCollection。

而當「by_reference」被評論沒有錯誤,但沒有任何也堅持ArrayCollection。

我的控制器:

public function creerProduitAction(Request $request) { 

    $produit = new Produit; 
    $formProduit = $this->createForm(ProduitType::class, $produit); 

    $formProduit->handleRequest($request); 

    if ($formProduit->isValid() && $formProduit->isSubmitted()) { 



     $em = $this->getDoctrine()->getManager(); 

     $em->persist($produit); 
     $em->flush(); 
    } 

    return $this->render('AdminBundle:Produit:creerProduit.html.twig', array(
     'formProduit' => $formProduit->createView() 
    )); 
} 

我以前收集在symfony 2.7而這個過程的工作。我使用symfony 2.8 atm。我不明白爲什麼收藏不會持久。

回答

0

首先,作爲Symfony's Docs說:

同樣,如果你使用的CollectionType場在您的底層集合數據對象(像學說的ArrayCollection的),然後by_reference必須設置爲false如果你需要加法器和去除器(例如addAuthor()和removeAuthor())來調用。

這正是您的情況,因此by_reference應設置爲false

至於你得到的錯誤,我敢肯定,你沒有必要的getters/setters,雖然你認爲你有。請檢查其名稱中是否存在任何拼寫錯誤。

+0

我修改了像這個garDegatEau這樣的變量名稱garDegatEaux。我有symfony要求的getters/setter:addGarDegatEau(\ ListeBundle \ Entity \ GarDegatEaux $ garDegatEau),以及remove和get。這是沒有更多的錯誤,但沒有任何承諾。我有antoher arraycollection沒有錯別字問題和相同的問題相同的配置 – stax