我在实体医生与实体体验和parcours之间具有一个OneToMany关系。现在,当我尝试保存表单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()
));
}
}
}
这是我的数据库的屏幕截图:
谢谢
答案 0 :(得分:1)
集合表单类型具有by_reference
选项。
https://symfony.com/doc/current/reference/forms/types/collection.html#by-reference
将此设置为false。
您的parcours
实体中有experiences
和Doctor
。您应该为此创建添加和删除方法,然后在此处设置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