代码之家  ›  专栏  ›  技术社区  ›  Dark Magic

如何在一对多关系Symfony中设置相关实体id?

  •  0
  • Dark Magic  · 技术社区  · 8 年前

    我试图坚持两个实体,有一对一和多对一的关系。

    我试图在表单中嵌入一组表单。

    除了数据数据表中的候选id之外,只有实体简历被成功创建和保存。

    • 简历表

    id | titre | candidate_id|id|id|u教育

    id\U教育具有以下价值:条令\公共\收藏\ArrayCollection@000000003d585f990000000052235238

    • 教育表为空

    我的问题是id\U简历和id\U教育。

    /**
     * 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;
    }
    

    简历控制员:

    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(),
            ));
        }
    }
    

    简历类型:

    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:'
                    ]
                )
            ;
        }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Khadija    8 年前

    /** * @var CurriculumVitae * @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\CurriculumVitae") *@ORM\JoinColumn(name="candidatId",referencedColumnName="id",onDelete="SET NULL") */ private $idCurriculumVitae;

        2
  •  0
  •   Dark Magic    8 年前

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

    必须在控制器中定义id。

    $curriculumVitae->setCandidat($candidat);