如何为一对多关系配置我的构建表单?



我正在尝试制作一个嵌套表单,在表单中嵌入表单集合。 我对构建表单有问题。

我正在尝试在其他表单类型中添加表单类型,以将教育添加到我的课程简历表单中。

  • 一个课程生活有很多教育。

  • 许多教育都有一个课程

我收到此错误消息:

类型为"数组或(\Traversable and \ArrayAccess)"的预期参数, 给定的"字符串">

课程简介实体:

/**
* CurriculumVitae
*
* @ORMTable(name="curriculum_vitae")
* @ORMEntity(repositoryClass="GECandidatBundleRepositoryCurriculumVitaeRepository")
*/
class CurriculumVitae
{
/**
* @ORMOneToMany(targetEntity="GECandidatBundleEntityEducation", mappedBy="curriculumVitae", cascade={"persist", "remove"})
* @ORMColumn(name="id_education")
*/
protected $educations;
/**
* Add education
*
* @param GECandidatBundleEntityEducation $education
*
* @return CurriculumVitae
*/
public function addEducation(GECandidatBundleEntityEducation $education)
{
$this->educations[] = $education;
$education->setCurriculumVitae($this);
return $this;
}
/**
* Remove education
*
* @param GECandidatBundleEntityEducation $education
*/
public function removeEducation(GECandidatBundleEntityEducation $education)
{
$this->educations->removeElement($education);
}
}

教育实体:

/**
* Education
*
* @ORMTable(name="education")
* @ORMEntity(repositoryClass="GECandidatBundleRepositoryEducationRepository")
*/
class Education
{
...
/**
* Many Education have One CurriculumVitae.
* @ORMManyToOne(targetEntity="GECandidatBundleEntityCurriculumVitae", inversedBy="educations")
* @ORMJoinColumn(name="curriculumVitae_id", referencedColumnName="id")
*/
private $curriculumVitae;
}

课程类型:

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:'
]
)
;
}

课程摘要控制器:

class CurriculumVitaeController extends Controller
{
...
public function editAction(Request $request, CurriculumVitae $curriculumVitae)
{
$deleteForm = $this->createDeleteForm($curriculumVitae);
$editForm = $this->createForm('GECandidatBundleFormCurriculumVitaeType', $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(),
));
}
}

我不知道是构造函数的问题还是什么?

在我的课程简介实体中:

/**
* Constructor
*/
public function __construct()
{
$this->objectifs = new DoctrineCommonCollectionsArrayCollection();
$this->educations = new DoctrineCommonCollectionsArrayCollection();
$this->experiences = new DoctrineCommonCollectionsArrayCollection();
$this->formations = new DoctrineCommonCollectionsArrayCollection();
$this->competences = new DoctrineCommonCollectionsArrayCollection();
$this->dateAjout = new Datetime();
$this->etat = false;
$this->disponibilite = false;
}

可能是问题就在这里,当我们需要多次嵌套单个表单时,我的意思是与哪些关系?

抱歉,

我找到了我的问题。

1)$candidatId应该$candidat。我们不是在实体中存储 ID,而是对$candidat的引用。

2) CoursericulumVitae中没有教育ID列。该关系存储在每个单独的教育实体中。

3)不需要@ORM\列(name="id_education")。

我添加了一个加入列来$idCurriculumVitae

/**
* @var CurriculumVitae
* @ORMManyToOne(targetEntity="GECandidatBundleEntityCurriculumVitae")
*@ORMJoinColumn(name="curriculumVitae_id",referencedColumnName="id",onDelete="SET NULL")
*/
private $idCurriculumVitae;

我有一个多对一的相关实体,每次我创建一个与Candidat实体相关的新CurriculumVitae实体时,candidat_id_id列为空。

仅成功创建和保留实体CurriculumVitae,数据表中的 Candidat ID 除外。

课程表

id | titre | candidat_id_id | id_education
  • candidat_id_id为:空
  • id_education具有以下值:DoctrineCommonCollectionsArrayCollection@000000003d585f990000000052235238

教育表为空

id | description | id_curriculum_vitae_id

我的问题在于id_curriculum_vitae_idid_education.

相关内容

  • 没有找到相关文章

最新更新