Dark Magic
Dark Magic

Reputation: 149

How to set related entity id in one to many relationship Symfony?

I am trying to persist two entities that have OneToMany and ManyToOne relationships.

I'm trying to embed a collection of forms inside a form.

I have a many to one related entities and everytime I create a new CurriculumVitae which is related to Candidat the column candidat_id_id is null.

Only the entity CurriculumVitae is successfully created and persisted except the Candidat id in the data datatable.

id | titre | candidat_id_id | id_education

candidat_id_id is: null

id_education has this value: Doctrine\Common\Collections\ArrayCollection@000000003d585f990000000052235238

id | description | id_curriculum_vitae_id

My problem is with the id_curriculum_vitae_id and the id_education.

CurriculumVitae Entity:

/**
 * 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 Entity:

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

Upvotes: 0

Views: 2920

Answers (2)

Dark Magic
Dark Magic

Reputation: 149

Sorry, i found my problem.

the id must be defined in the Controller.

$curriculumVitae->setCandidat($candidat);

Upvotes: 0

Khadija
Khadija

Reputation: 310

Try this code and update your database's schema /** * @var CurriculumVitae * @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\CurriculumVitae") *@ORM\JoinColumn(name="candidatId",referencedColumnName="id",onDelete="SET NULL") */ private $idCurriculumVitae;

Upvotes: 0

Related Questions