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

时间:2017-09-26 09:47:52

标签: symfony doctrine-orm doctrine

我试图坚持两个拥有OneToMany和ManyToOne关系的实体。

我正在尝试在表单中嵌入一组表单。

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

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

  • CurriculumVitae Table

id | titre | candidat_id_id | id_education

candidat_id_id是:null

id_education具有以下值:Doctrine \ Common \ Collections \ ArrayCollection @ 000000003d585f990000000052235238

  • 教育表是空的

id |描述| id_curriculum_vitae_id

我的问题在于id_curriculum_vitae_id和id_education。

CurriculumVitae实体:

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

抱歉,我发现了我的问题。

必须在Controller中定义id。

$curriculumVitae->setCandidat($candidat);