2017-09-26 92 views
0

我想坚持两个有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:' 
       ] 
      ) 
     ; 
    } 
+0

几个问题。从概念上讲,$ candidatId应该是$ candidat。您不是在实体中存储ID,而是参考$ candidat。重要的是了解这一点。您的代码中没有任何内容表明候选人正在为CurriculumVitae设置。 – Cerad

+0

关于教育,您不会在CurriculumVitae中有教育ID列。这种关系存储在每个教育实体中。所以摆脱@ORM \ Column(name =“id_education”)。你的addEducation方法看起来是正确的。事实上,你似乎没有得到任何新的教育对象可能是JavaScript的问题。我只能说通过文档中的例子。 – Cerad

+0

这解决了edite表单中的问题,但添加表单仍然相同。 candidat_id仍然为空,现在没有culum id_education。 –

回答

0

对不起,我发现我的问题。

该ID必须在控制器中定义。

$curriculumVitae->setCandidat($candidat); 
0

试试这个代码,并更新数据库的架构 /** * @var CurriculumVitae * @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\CurriculumVitae") *@ORM\JoinColumn(name="candidatId",referencedColumnName="id",onDelete="SET NULL") */ private $idCurriculumVitae;

+0

它不起作用,它仍然是同样的结果。我试图在表单内嵌入一组表单。 –

+0

@ORM \ JoinColumn(name =“curriculumVitae_id”,referencedColumnName =“id”,onDelete =“SET NULL”) –

相关问题