我想堅持兩個有OneToMany和ManyToOne關係的實體。如何在一對多的關係中設置相關的實體標識Symfony?
我想嵌入窗體內的窗體集合。
我有一個多對一的相關實體,每次創建一個與Candidat相關的新CurriculumVita列candidat_id_id爲null。
只有實體CurriculumVitae已成功創建並保持,但數據數據表中的Candidat ID除外。
- 簡歷表
ID | titre | candidat_id_id | id_education
candidat_id_id是:空
id_education有此值:學說\ COMMON \收藏\ ArrayCollection的@ 000000003d585f990000000052235238
- 教育表是空
ID |描述| id_curriculum_vitae_id
我的問題是與id_curriculum_vitae_id和id_education。
簡歷實體:
/**
* CurriculumVitae
*
* @ORM\Table(name="curriculum_vitae")
* @ORM\Entity(repositoryClass="GE\CandidatBundle\Repository\CurriculumVitaeRepository")
*/
class CurriculumVitae
{
/**
* @var Candidat
*
* @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\Candidat")
*/
protected $candidatId;
/**
* @var Education
* @ORM\OneToMany(targetEntity="GE\CandidatBundle\Entity\Education", mappedBy="idCurriculumVitae", 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
{
...
/**
* @var CurriculumVitae
* @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\CurriculumVitae")
*/
private $idCurriculumVitae;
}
CurriculumVitaeController:
class CurriculumVitaeController extends Controller
{
/**
* Creates a new curriculumVitae entity.
*
* @Route("/candidat/cv/ajouter", name="cv_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$curriculumVitae = new Curriculumvitae();
$form = $this->createForm('GE\CandidatBundle\Form\CurriculumVitaeType', $curriculumVitae);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($curriculumVitae);
$em->flush();
$request->getSession()
->getFlashBag()
->add('infoCv', 'Votre cv a été bien enregistrée.');
return $this->redirectToRoute('cv_show', array('id' => $curriculumVitae->getId()));
}
return $this->render('curriculumvitae/new.html.twig', array(
'curriculumVitae' => $curriculumVitae,
'form' => $form->createView(),
));
}
}
CurriculumVitaeType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('candidat', EntityType::class, array(
'class' => 'GECandidatBundle:Candidat',
'choice_label' => 'id',
'multiple' => false,
))
->add('educations',
CollectionType::class, [
'entry_type' => EducationType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'label' => 'Educations:'
]
)
;
}
幾個問題。從概念上講,$ candidatId應該是$ candidat。您不是在實體中存儲ID,而是參考$ candidat。重要的是瞭解這一點。您的代碼中沒有任何內容表明候選人正在爲CurriculumVitae設置。 – Cerad
關於教育,您不會在CurriculumVitae中有教育ID列。這種關係存儲在每個教育實體中。所以擺脫@ORM \ Column(name =「id_education」)。你的addEducation方法看起來是正確的。事實上,你似乎沒有得到任何新的教育對象可能是JavaScript的問題。我只能說通過文檔中的例子。 – Cerad
這解決了edite表單中的問題,但添加表單仍然相同。 candidat_id仍然爲空,現在沒有culum id_education。 –