2014-04-10 72 views
0

我很難保存包含OneToMany雙向關係集合的表單。形式中的雙向關係

我得到了以下錯誤:

An exception occurred while executing 'INSERT INTO fieldsgroup (title, colloque_id) VALUES (?, ?)' with params ["groupe 1", null]: 

以下是的查詢分析器給我: START TRANSACTION;

INSERT INTO colloque (title) 
VALUES 
    ('titre du colloque'); 

INSERT INTO fieldsgroup (title, colloque_id) 
VALUES 
    ('titre de mon groupe de champs', null) 

ROLLBACK  

我的控制器:

public function createColloqueAction() 
{ 

    $colloque = new Colloque(); 
    $form = $this->createForm(new ColloqueType, $colloque); 

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

    if ($form->isValid()) { 

     $this->get('session')->getFlashBag()->add('notice', 'Colloque correctement enregistré'); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($colloque); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('ptolemee_admin_homepage')); 
    } 

    }  

    return $this->render('PtolemeeAdminBundle:Admin:createColloque.html.twig',array(
    'form' => $form->createView()           
)); 
} 

我 「Colloque」 實體:

class Colloque 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\OneToMany(targetEntity="Ptolemee\ColloqueBundle\Entity\FieldsGroup", mappedBy="colloque", cascade={"persist"}) 
    */ 
    private $fieldsGroups; 

    public function __construct() 
    { 
     $this->fieldsGroups = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Add fieldsGroups 
    * 
    * @param \Ptolemee\ColloqueBundle\Entity\FieldsGroup $fieldsGroups 
    * @return Colloque 
    */ 
    public function addFieldsGroup(\Ptolemee\ColloqueBundle\Entity\FieldsGroup $fieldsGroups) 
    { 
     $this->fieldsGroups[] = $fieldsGroups; 
     $fieldsGroups->setColloque($this); 
     return $this; 
    } 

    /** 
    * Remove fieldsGroups 
    * 
    * @param \Ptolemee\ColloqueBundle\Entity\FieldsGroup $fieldsGroups 
    */ 
    public function removeFieldsGroup(\Ptolemee\ColloqueBundle\Entity\FieldsGroup $fieldsGroups) 
    { 
     $this->fieldsGroups->removeElement($fieldsGroups); 
    } 

    /** 
    * Get fieldsGroups 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getFieldsGroups() 
    { 
     return $this->fieldsGroups; 
    } 
} 

它的形式,ColloqueType:

class ColloqueType extends AbstractType 
{ 
     /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title',    'text') 
      ->add('date',    'datetime') 
      ->add('successMessage',  'textarea') 
      ->add('fieldsGroups',  'collection', array( 'type'  =>  new FieldsGroupType(), 
                   'allow_add' =>  true, 
                   'allow_delete' => true)) 
     ; 
    } 
} 

我班FieldsGroup

class FieldsGroup 
{ 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /**                     
    * @ORM\ManyToOne(targetEntity="Ptolemee\ColloqueBundle\Entity\Colloque", inversedBy="fieldsGroups") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $colloque; 

    /** 
    * Set colloque 
    * 
    * @param \Ptolemee\ColloqueBundle\Entity\Colloque $colloque 
    * @return FieldsGroup 
    */ 
    public function setColloque(\Ptolemee\ColloqueBundle\Entity\Colloque $colloque) 
    { 
     $this->colloque = $colloque; 

     return $this; 
    } 

    /** 
    * Get colloque 
    * 
    * @return \Ptolemee\ColloqueBundle\Entity\Colloque 
    */ 
    public function getColloque() 
    { 
     return $this->colloque; 
    } 

} 

而且它的形式,FieldsGroupType:

class FieldsGroupType extends AbstractType 
{ 
     /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title',  'text') 
      /*->add('fields',  'collection', array('type'  =>  new FieldType(), 
                 'allow_add' =>  true, 
                 'allow_delete' =>  true))*/ 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Ptolemee\ColloqueBundle\Entity\FieldsGroup' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'ptolemee_colloquebundle_fieldsgroup'; 
    } 
} 

我知道這應該沒有任何更多perist(),或任何其他正常工作...... 任何幫助,將不勝感激,我一直在那工作幾個小時,卻找不到什麼是正確的方式......

非常感謝!

回答

0

只需添加setFieldsGroup(ArrayCollection中)到你的實體

 public function addFieldsGroup(\Ptolemee\ColloqueBundle\Entity\FieldsGroup $fieldsGroups) 
     { 
      $this->fieldsGroups[] = $fieldsGroups; 
      $fieldsGroups->setColloque($this); 
      return $this; 
     } 

    public function setFieldsGroup(ArrayCollection $fieldsGroups) 
     { 
      foreach ($fieldsGroups as $fieldsGroup) { 
       $fieldsGroup->setColloque($this); 
      } 
      $this->fieldsGroups = $fieldsGroups; 
     } 
+0

您好,感謝您的幫助,但是這不工作... 然而,我發現,如果去除$ this-> fieldsGroups = new \ Doctrine \ Common \ Collections \ ArrayCollection();在我的Colloque的構造函數中,這是工作,但我不明白爲什麼......我是新入symfony和我已經遵循的教訓指定,我必須做雙向關係... – nikophil

0

添加setFieldsGroup(ArrayCollection的)工作,但它是一個把戲。

只需添加, cascade={"persist"}您多對一場

/**                     
* @ORM\ManyToOne(targetEntity="Ptolemee\ColloqueBundle\Entity\Colloque", inversedBy="fieldsGroups", cascade={"persist"}) 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $colloque; 

PS1: 體型的handleRequest(),而不是$request->getMethod() == 'POST'$form->bind($request)如果你有Symfony2.x X> = 3。

PS2: 你可以像這樣在需要時(更優雅的方式)要求:

public function createColloqueAction(Request $request) 
+0

你好,謝謝但是,這是行不通的... 但是,我發現當刪除$ this-> fieldsGroups = new \ Doctrine \ Common \ Collections \ ArrayCollection();在我的Colloque的構造函數中,這是工作,但我不明白爲什麼... 我是新入symfony和我遵循的教訓指定我必須做雙向關係...... 無論如何,我把這個級聯= {「堅持」},因爲這是我認爲的好方法。 – nikophil

+0

你應該讓默認的構造函數。如果它阻止了正常的功能,那麼你在其他地方有一個錯誤。 其餘的是好的,只是添加級聯堅持不工作? – Healkiss

+0

是的,在這種情況下級聯堅持不會改變任何東西。刪除此$ this-> fieldsGroups = new ArrayCollection()是刪除錯誤的操作。但我真的不知道爲什麼,我認爲使用雙向關係時需要這個新的ArrayCollection()... – nikophil