我很難保存包含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(),或任何其他正常工作...... 任何幫助,將不勝感激,我一直在那工作幾個小時,卻找不到什麼是正確的方式......
非常感謝!
您好,感謝您的幫助,但是這不工作... 然而,我發現,如果去除$ this-> fieldsGroups = new \ Doctrine \ Common \ Collections \ ArrayCollection();在我的Colloque的構造函數中,這是工作,但我不明白爲什麼......我是新入symfony和我已經遵循的教訓指定,我必須做雙向關係... – nikophil