将数据持久化到 symfony 中的 OneToMany 实体中时出现问题

Problem persisting data into a OneToMany entity in symfony

我在实体医生和实体体验之间存在 OneToMany 关系,parcours.Now 当我尝试保存我的表单 ExperiencesType 和 ParcoursType 的数据时,除了医生的 ID 之外,所有数据都被保存。

实体医生

/**
 * @ORM\OneToMany(targetEntity="Doctix\MedecinBundle\Entity\Experiences", mappedBy="medecin",  cascade={"persist", "remove"})
 */
private $experiences;

/**
 * @ORM\OneToMany(targetEntity="Doctix\MedecinBundle\Entity\Parcours", mappedBy="medecin", cascade={"persist", "remove"})
 */
private $parcours;

 public function __construct()
{
    $this->assurance = new \Doctrine\Common\Collections\ArrayCollection();
    $this->experiences =  new \Doctrine\Common\Collections\ArrayCollection();
    $this->parcours =  new \Doctrine\Common\Collections\ArrayCollection();
}

  /**
 * Get experiences
 *
 * @return \Doctix\MedecinBundle\Entity\Experiences
 */
public function getExperiences()
{
    return $this->experiences;
}

  /**
 * Get parcours
 *
 * @return \Doctix\MedecinBundle\Entity\Parcours
 */
public function getParcours()
{
    return $this->parcours;
}

实体体验

     /**
 * @ORM\ManyToOne(targetEntity="Doctix\MedecinBundle\Entity\Medecin", inversedBy="experiences")
 * @ORM\JoinColumn(name="medecin_id", referencedColumnName="id" , nullable=true)
 */
private $medecin;

  /**
 * Set medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 *
 * @return Experiences
 */
public function setMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin = null)
{
    $this->medecin = $medecin;

    return $this;
}

/**
 * Get medecin
 *
 * @return \Doctix\MedecinBundle\Entity\Medecin
 */
public function getMedecin()
{
    return $this->medecin;
}

实体店

  /**
 * @ORM\ManyToOne(targetEntity="Doctix\MedecinBundle\Entity\Medecin", inversedBy="parcours")
 * @ORM\JoinColumn(name="medecin_id", referencedColumnName="id")
 */
private $medecin;

  /**
 * Set medecin
 *
 * @param \Doctix\MedecinBundle\Entity\Medecin $medecin
 *
 * @return Parcours
 */
public function setMedecin(\Doctix\MedecinBundle\Entity\Medecin $medecin = null)
{
    $this->medecin = $medecin;

    return $this;
}

/**
 * Get medecin
 *
 * @return \Doctix\MedecinBundle\Entity\Medecin
 */
public function getMedecin()
{
    return $this->medecin;
}

那么这是我的表格: MedecinCvEditType

  class MedecinCvEditType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->remove('user')
            ->remove('specialite')
            ->add('experiences', CollectionType::class, array(
              'entry_type' => ExperienceType::class,
              'allow_add' => true,
              'allow_delete' => true
          ))
            ->add('parcours', CollectionType::class, array(
              'entry_type' => ParcoursType::class,
              'allow_add' => true,
              'allow_delete' => true
          ));
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Doctix\MedecinBundle\Entity\Medecin'
    ));
}

  }

最后是我的控制器:

控制器

  public function cvEditAction(Request $request)
{
    $user = $this->getUser();
    if ($user === null) {
        throw new NotFoundHttpException('Utilisateur Inexistant');
    } else {
        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository('DoctixMedecinBundle:Medecin');
        $medecin = $repo->findOneBy(array(
            'user' => $user,
        ));
        $form = $this->createForm('Doctix\MedecinBundle\Form\MedecinCvEditType', $medecin);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

      $em->flush();
            // creation d'un flash bag pour une notification flash
            $request->getSession()->getFlashBag()->add('Notice', 'Cv Modifié avec succés');
            // redirection
            $url = $this->generateUrl('medecin_parametre');
            // redirection permanente avec le status http 301 ::)))))
            return $this->redirect($url, 301);

        } else {
            return $this->render('DoctixMedecinBundle:Medecin:cvedit.html.twig', array(
                'form' => $form->createView()
            ));
        }
    }

}

这是我的数据库的截图:

谢谢

集合表单类型有一个 by_reference 选项。

https://symfony.com/doc/current/reference/forms/types/collection.html#by-reference

将此设置为 false。

您的 Doctor 实体中有 parcoursexperiences。您应该为这些创建添加和删除方法,并在那里设置 Doctor。

示例:

/**
 * @return Collection|Experiences[]
 */
public function getExperiences(): Collection
{
    return $this->experiences;
}

public function addExperience(Experiences $exp): self
{
    $exp->setDoctor($this);

    $this->experiences->add($exp);

    return $this;
}

public function removeExperience(Experiences $exp): self
{
    $exp->setDoctor(null);

    $this->experiences->removeElement($exp);

    return $this;
}

另外,阅读孤儿删除:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-associations.html#orphan-removal