我尝试制作嵌套表单,在表单中嵌入表单集合。 我有buildForm的问题。
我尝试在我的案例中添加其他表单类型的formType,以便为我的CurriculumVitae表单添加教育。
一个CurriculumVitae有很多教育。
许多教育都有一个简历。
我收到此错误消息:
类型"数组或(\ Traversable和\ ArrayAccess)"的预期参数, "串"给定
CurriculumVitae实体:
/**
* CurriculumVitae
*
* @ORM\Table(name="curriculum_vitae")
* @ORM\Entity(repositoryClass="GE\CandidatBundle\Repository\CurriculumVitaeRepository")
*/
class CurriculumVitae
{
/**
* @ORM\OneToMany(targetEntity="GE\CandidatBundle\Entity\Education", mappedBy="curriculumVitae", cascade={"persist", "remove"})
* @ORM\Column(name="id_education")
*/
protected $educations;
/**
* Add education
*
* @param \GE\CandidatBundle\Entity\Education $education
*
* @return CurriculumVitae
*/
public function addEducation(\GE\CandidatBundle\Entity\Education $education)
{
$this->educations[] = $education;
$education->setCurriculumVitae($this);
return $this;
}
/**
* Remove education
*
* @param \GE\CandidatBundle\Entity\Education $education
*/
public function removeEducation(\GE\CandidatBundle\Entity\Education $education)
{
$this->educations->removeElement($education);
}
}
教育实体:
/**
* Education
*
* @ORM\Table(name="education")
* @ORM\Entity(repositoryClass="GE\CandidatBundle\Repository\EducationRepository")
*/
class Education
{
...
/**
* Many Education have One CurriculumVitae.
* @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\CurriculumVitae", inversedBy="educations")
* @ORM\JoinColumn(name="curriculumVitae_id", referencedColumnName="id")
*/
private $curriculumVitae;
}
CurriculumVitaeType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('educations',
CollectionType::class, [
'entry_type' => EducationType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'label' => 'Educations:'
]
)
;
}
CurriculumVitaeController:
class CurriculumVitaeController extends Controller
{
...
public function editAction(Request $request, CurriculumVitae $curriculumVitae)
{
$deleteForm = $this->createDeleteForm($curriculumVitae);
$editForm = $this->createForm('GE\CandidatBundle\Form\CurriculumVitaeType', $curriculumVitae);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('cv_edit', array('id' => $curriculumVitae->getId()));
}
return $this->render('curriculumvitae/edit.html.twig', array(
'curriculumVitae' => $curriculumVitae,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
}
我不知道构造函数是否存在问题或者是什么?
在我的CurriculumVitae实体中:
/**
* Constructor
*/
public function __construct()
{
$this->objectifs = new \Doctrine\Common\Collections\ArrayCollection();
$this->educations = new \Doctrine\Common\Collections\ArrayCollection();
$this->experiences = new \Doctrine\Common\Collections\ArrayCollection();
$this->formations = new \Doctrine\Common\Collections\ArrayCollection();
$this->competences = new \Doctrine\Common\Collections\ArrayCollection();
$this->dateAjout = new \Datetime();
$this->etat = false;
$this->disponibilite = false;
}
可能问题出在这里,当我们需要不止一次地嵌套单个表单时,我的意思是与哪些关系?
答案 0 :(得分:0)
我有多对一的相关实体,每次创建与CurriculumVitae
实体相关的新Candidat
实体时,列candidat_id_id
都为空。
除了数据数据表中的Candidat id之外,仅成功创建并保留了实体CurriculumVitae
。
CurriculumVitae 表:
id | titre | candidat_id_id | id_education
Doctrine\Common\Collections\ArrayCollection@000000003d585f990000000052235238
教育表是空的
id | description | id_curriculum_vitae_id
我的问题在于id_curriculum_vitae_id
和id_education
。
答案 1 :(得分:0)
对不起,
我发现了我的问题。
1)$ candidatId应该是$ candidat。我们不是在实体中存储ID,而是对$ candidat的引用。
2)CurriculumVitae中没有教育ID专栏。该关系存储在每个单独的教育实体中。
3)不需要@ORM \ Column(name =" id_education")。
我添加了一个JoinColumn到$ idCurriculumVitae
/**
* @var CurriculumVitae
* @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\CurriculumVitae")
*@ORM\JoinColumn(name="curriculumVitae_id",referencedColumnName="id",onDelete="SET NULL")
*/
private $idCurriculumVitae;