2013-06-21 6 views
0

배열 컬렉션에 문제가 있습니다.Symfony2 Doctrine2 배열 컬렉션 OneToMany가 항목을 추가하지 않습니다.

"$ livraison-> setChoix ($ livraison-> getChoix());"를 사용하지 않으면 " 유효한 형태로 항목은 관계에 저장되지 않습니다. 그리고 이것으로, 어떤 저장에서나 중복으로 컬렉션의 항목.

난 "Livraison"및 "LivraisonChoix"2 엔티티가

Livraison 이것이 Livraison이다 LivraisonChoix가 Livraison

와 관계 ManyToOne에 LivraisonChoix와 OneToMany 관계이다

... 
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

xx. 그러나이 문제는 이미 여기에 있습니다. 양식 데이터가 저장되지만 "LivraisonChoix"의 "livraison"값이 "null"입니다. 저장시 "Livraison"의 "addChoix"메서드가 사용되지 않습니다./ –

+0

나는 내 대답을 편집합니다. 다음을 추가해야한다고 생각합니다. 'by_reference'=> false, – Sybio

+0

예! 고마워 ! 양식에서 항목을 제거하면 항목이 제거되지 않습니다. 그 이유는 무엇입니까? 매우 thx! –