2013-06-21 83 views
0

我有數組集合的問題。Symfony2 Doctrine2數組集合OneToMany不添加項目

如果我不使用「$ livraison-> setChoix($ livraison-> getChoix());」在表格中有效,該項目不保存在關係中。 與此,在任何保存中的集合中的項目重複。

我有2個實體, 「Livraison」 和 「LivraisonChoix」

Livraison是相對於一對多與LivraisonChoix LivraisonChoix與Livraison

關係多對一這是Livraison:

... 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

class Livraison 
{ 

... 

    /** 
    * @ORM\OneToMany(targetEntity="\YOU\CommercantBundle\Entity\LivraisonChoix", mappedBy="livraison", cascade={"all"}) 
    **/ 
    private $choix; 

    public function __construct() 
    { 
     $this->choix = new ArrayCollection(); 
    } 


    public function addChoix(\YOU\CommercantBundle\Entity\LivraisonChoix $choix) 
    { 
     $choix->setLivraison($this); 
     $this->choix[] = $choix; 
    } 

    public function setChoix($choix) 
    { 
     foreach($choix as $choi){ 
      $this->addChoix($choi); 
     } 
    } 

    public function removeChoix($choix) 
    { 
     $this->choix->removeElement($choix); 
    } 

    public function getChoix() 
    { 
     return $this->choix; 
    } 

... 

這是LivraisonChoix:

use Doctrine\ORM\Mapping as ORM; 

class LivraisonChoix 
{ 

... 

    /** 
    * @ORM\ManyToOne(targetEntity="YOU\CommercantBundle\Entity\Livraison", inversedBy="choix") 
    **/ 
    private $livraison; 

... 


    public function setLivraison($livraison) 
    { 
     $this->livraison = $livraison; 

     return $this; 
    } 

    public function getLivraison() 
    { 
     return $this->livraison; 
    } 

... 

這是表單生成器:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('name') 
     ->add('choix','collection',array(
         'type'=>new LivraisonChoixType(), 
         'allow_add' => true, 
         'allow_delete' => true, 
     )) 
    ; 
} 

這是控制器:

 $livraison = new Livraison(); 

     $form = $this->createForm(new LivraisonType(), $livraison); 

     $request = $this->get('request'); 
     if ($request->getMethod() == 'POST') { 
      $form->bind($request); 

      if ($form->isValid()) { 

       $livraison->setAccount($customer); 
       $livraison->setChoix($livraison->getChoix()); 
       $em->persist($livraison); 
       $em->flush(); 

       return $this->redirect($this->generateUrl('you_commercant_livraison_editer',array('id'=>$livraison->getId()))); 

      } 
     } 

回答

0

你忘了一件重要的事,得到了新的 「livraison」($form->getData())的形式提交後:

if ($form->isValid()) { 

    $livraison = $form->getData(); // You forgot to get the new/edited "livraison" 

    $livraison->setAccount($customer); 
    $em->persist($livraison); 
    $em->flush(); 

    return $this->redirect($this->generateUrl('you_commercant_livraison_editer',array('id'=>$livraison->getId()))); 
} 

編輯:

我認爲你應該在表單字段中添加'by_reference'設置爲false,這將調用addChoix()方法!檢查這個cookbook(在本部分的末尾)。這裏的細節by_reference

$builder->add('choix', 'collection', array(
    // ... 
    'by_reference' => false, 
)); 
+0

thx。 但這個問題已經在這裏。 表單數據保存,但「livraison」的值在「LivraisonChoix」中爲「null」 「Livraison」中的「addChoix」方法不用於保存:/ –

+0

我編輯我的答案,I認爲你需要添加:'by_reference'=> false, – Sybio

+0

是啊!謝謝 !當我刪除表單中的項目時,該項目不會被刪除,你知道爲什麼嗎?非常thx! –