2014-07-25 28 views
0

我想在我的表格刪除一個項目,但什麼也沒有發生(甚至不是一個錯誤......但沒有問題,我可以編輯!)Symfony的2 - 形式收集從一對多的關係刪除項目

我有

在一對多

名爲「MentionLegaleNv1」第一級
class MentionLegaleNv1 { 

    /** 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(name="titre", type="string", length=50, nullable=false) 
    */ 
    private $titre; 

    /** 
    * @ORM\OneToMany(targetEntity="MentionLegaleNv2", mappedBy="MentionsLegalesNv1", cascade={"persist", "remove"}) 
    */ 
    private $MentionsLegalesNv2; 

    /** 
    * Constructor 
    */ 
    public function __construct() { 
     $this->MentionsLegalesNv2 = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() { 
     return $this->id; 
    } 

    /** 
    * Set titre 
    * 
    * @param string $titre 
    * @return MentionLegaleNv1 
    */ 
    public function setTitre($titre) { 
     $this->titre = $titre; 

     return $this; 
    } 

    /** 
    * Get titre 
    * 
    * @return string 
    */ 
    public function getTitre() { 
     return $this->titre; 
    } 

    /** 
    * Add MentionsLegalesNv2 
    * 
    * @param \Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2 
    * @return MentionLegaleNv1 
    */ 
    public function addMentionsLegalesNv2(\Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2) { 
     $mentionsLegalesNv2->setMentionLegaleNv1($this); 
     $this->MentionsLegalesNv2[] = $mentionsLegalesNv2; 

     return $this; 
    } 

    /** 
    * Remove MentionsLegalesNv2 
    * 
    * @param \Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2 
    */ 
    public function removeMentionsLegalesNv2(\Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2) { 
     $this->MentionsLegalesNv2->removeElement($mentionsLegalesNv2); 
    } 

    /** 
    * Get MentionsLegalesNv2 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getMentionsLegalesNv2() { 
     return $this->MentionsLegalesNv2; 
    } 
:有3個級別(這是關於劃分子級我的網站的法律聲明)

這裏是我的實體嵌入表單的Collection

第二名爲 「MentionLegaleNv2」 的多對一和一對多

class MentionLegaleNv2 { 

    /** 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(name="titre", type="string", length=50, nullable=false) 
    */ 
    private $titre; 

    /** 
    * @ORM\OneToMany(targetEntity="MentionLegaleNv3", mappedBy="MentionsLegalesNv2", cascade={"persist", "remove"}) 
    */ 
    private $MentionsLegalesNv3; 

    /** 
    * @ORM\ManyToOne(targetEntity="MentionLegaleNv1", inversedBy="MentionsLegalesNv2") 
    */ 
    private $MentionsLegalesNv1; 

    /** 
    * Constructor 
    */ 
    public function __construct() { 
     $this->MentionsLegalesNv3 = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() { 
     return $this->id; 
    } 

    /** 
    * Set titre 
    * 
    * @param string $titre 
    * @return MentionLegaleNv2 
    */ 
    public function setTitre($titre) { 
     $this->titre = $titre; 

     return $this; 
    } 

    /** 
    * Get titre 
    * 
    * @return string 
    */ 
    public function getTitre() { 
     return $this->titre; 
    } 

    /** 
    * Add MentionsLegalesNv3 
    * 
    * @param \Video2Learn\BddBundle\Entity\MentionLegaleNv3 $mentionsLegalesNv3 
    * @return MentionLegaleNv2 
    */ 
    public function addMentionsLegalesNv3(\Video2Learn\BddBundle\Entity\MentionLegaleNv3 $mentionsLegalesNv3) { 
     $mentionsLegalesNv3->setMentionLegaleNv2($this); 
     $this->MentionsLegalesNv3[] = $mentionsLegalesNv3; 

     return $this; 
    } 

    /** 
    * Remove MentionsLegalesNv3 
    * 
    * @param \Video2Learn\BddBundle\Entity\MentionLegaleNv3 $mentionsLegalesNv3 
    */ 
    public function removeMentionsLegalesNv3(\Video2Learn\BddBundle\Entity\MentionLegaleNv3 $mentionsLegalesNv3) { 
     $this->MentionsLegalesNv3->removeElement($mentionsLegalesNv3); 
    } 

    /** 
    * Get MentionsLegalesNv3 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getMentionsLegalesNv3() { 
     return $this->MentionsLegalesNv3; 
    } 

    /** 
    * Set MentionsLegalesNv1 
    * 
    * @param \Video2Learn\BddBundle\Entity\MentionLegaleNv1 $mentionsLegalesNv1 
    * @return MentionLegaleNv2 
    */ 
    public function setMentionsLegalesNv1(\Video2Learn\BddBundle\Entity\MentionLegaleNv1 $mentionsLegalesNv1 = null) { 
     $this->MentionsLegalesNv1 = $mentionsLegalesNv1; 

     return $this; 
    } 

    /** 
    * Get MentionsLegalesNv1 
    * 
    * @return \Video2Learn\BddBundle\Entity\MentionLegaleNv1 
    */ 
    public function getMentionsLegalesNv1() { 
     return $this->MentionsLegalesNv1; 
    } 

名爲 「MentionLegaleNv3」 在多對一

class MentionLegaleNv3 { 

    /** 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(name="titre", type="string", length=50, nullable=false) 
    */ 
    private $titre; 

    /** 
    * @ORM\Column(name="description", type="string", length=500, nullable=false) 
    */ 
    private $description; 

    /** 
    * @ORM\Column(name="removeTuto", type="boolean", nullable=false) 
    */ 
    private $removeTuto; 

    /** 
    * @ORM\ManyToOne(targetEntity="MentionLegaleNv2", inversedBy="MentionsLegalesNv3") 
    * @ORM\JoinColumn(referencedColumnName="id", nullable=false) 
    */ 
    private $MentionsLegalesNv2; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set titre 
    * 
    * @param string $titre 
    * @return MentionLegaleNv3 
    */ 
    public function setTitre($titre) 
    { 
     $this->titre = $titre; 

     return $this; 
    } 

    /** 
    * Get titre 
    * 
    * @return string 
    */ 
    public function getTitre() 
    { 
     return $this->titre; 
    } 

    /** 
    * Set description 
    * 
    * @param string $description 
    * @return MentionLegaleNv3 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

    /** 
    * Get description 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * Set removeTuto 
    * 
    * @param boolean $removeTuto 
    * @return MentionLegaleNv3 
    */ 
    public function setRemoveTuto($removeTuto) 
    { 
     $this->removeTuto = $removeTuto; 

     return $this; 
    } 

    /** 
    * Get removeTuto 
    * 
    * @return boolean 
    */ 
    public function getRemoveTuto() 
    { 
     return $this->removeTuto; 
    } 

    /** 
    * Set MentionsLegalesNv2 
    * 
    * @param \Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2 
    * @return MentionLegaleNv3 
    */ 
    public function setMentionsLegalesNv2(\Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2) 
    { 
     $this->MentionsLegalesNv2 = $mentionsLegalesNv2; 

     return $this; 
    } 

    /** 
    * Get MentionsLegalesNv2 
    * 
    * @return \Video2Learn\BddBundle\Entity\MentionLegaleNv2 
    */ 
    public function getMentionsLegalesNv2() 
    { 
     return $this->MentionsLegalesNv2; 
    } 

這裏是FormType最後一個級別:

MentionsLegalesNv1Type:

class MentionLegaleNv1Type extends AbstractType { 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
       ->add('MentionsLegalesNv2', 'collection', array(
        'type' => new MentionLegaleNv2Type(), 
        'prototype' => true, 
        'by_reference' => false, 
        'allow_add' => true, 
        'allow_delete' => true, 
        'error_bubbling' => false, 
        'label' => false, 
        'cascade_validation' => true, 
        'options' => array(
         'label' => false, 
        ), 
        'attr' => array(
         'class' => 'MentionsLegalesNv2 list-unstyled', 
        ) 
       )) 
       ->add('Valider', 'submit', array(
        'attr' => array(
         'class' => 'suivant btn btn-success', 
         'formnovalidate' => 'formnovalidate', 
        ) 
       )) 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) { 
     $resolver->setDefaults(array(
      'data_class' => 'Video2Learn\BddBundle\Entity\MentionLegaleNv1', 
      'cascade_validation' => true, 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() { 
     return 'video2learn_administrationbundle_mentionlegalenv1'; 
    } 

} 

MentionsLegalesNv2Type:

class MentionLegaleNv2Type extends AbstractType { 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
       ->add('titre', 'text', array(
        'required' => true, 
        'label' => false, 
        'attr' => array(
         'placeholder' => 'Mention Légales NV1 n°#', 
         'class' => 'form-control', 
         'title' => 'Titre de la Mention Legale NV1 n°#' 
        ) 
       )) 
       ->add('MentionsLegalesNv3', 'collection', array(
        'type' => new MentionLegaleNv3Type(), 
        'prototype' => true, 
        'by_reference' => false, 
        'allow_add' => true, 
        'cascade_validation' => true, 
        'allow_delete' => true, 
        'error_bubbling' => false, 
        'label' => false, 
        'options' => array(
         'label' => false // sert à supprimer les index 
        ), 
        'attr' => array(
         'class' => 'MentionsLegalesNv3 list-unstyled', 
        ) 
       )) 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) { 
     $resolver->setDefaults(array(
      'data_class' => 'Video2Learn\BddBundle\Entity\MentionLegaleNv2', 
      'cascade_validation' => true, 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() { 
     return 'video2learn_administrationbundle_mentionlegalenv2'; 
    } 

} 

MentionsLegalesNv3Type:

class MentionLegaleNv3Type extends AbstractType { 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
       ->add('titre', 'text', array(
        'required' => true, 
        'label' => 'Titre Paragraphe', 

       )) 
       ->add('description','textarea', array(
        'required' => true, 
        'label' => 'Description Paragraphe', 

       )) 
       ->add('removeTuto') 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) { 
     $resolver->setDefaults(array(
      'data_class' => 'Video2Learn\BddBundle\Entity\MentionLegaleNv3', 
      'cascade_validation' => true, 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() { 
     return 'video2learn_administrationbundle_mentionlegalenv3'; 
    } 

} 

而且我的控制器:

class MentionsLegalesController extends Controller { 

    public function mentionLegalesAction(Request $request) { 

     $em = $this->getDoctrine()->getManager(); 

     $ML = $em->getRepository('Video2LearnBddBundle:MentionLegaleNv0')->findOneById(1); 

     if ($request->getMethod() == 'GET') { 

       $form = $this->createForm(new MentionLegaleNv0Type(), $ML); 
     } 

     if ($request->isMethod('POST')) { 
      $form = $this->createForm(new MentionLegaleNv0Type(), $ML); 
      $form->handleRequest($request); 

      if ($form->isValid()) { 
       $ML = $form->getData(); 
       $em->persist($ML); 
       $em->flush(); 
       return $this->redirect($this->generateUrl('administration_mentions_legales')); 
      } 
     } 

     return $this->render('Video2LearnAdministrationBundle:Pages:mentions_legales.html.twig', array(
        'form' => $form->createView(), 

     )); 
    } 

} 

我發現的Symfony的食譜(http://symfony.com/doc/current/cookbook/form/form_collections.html),我試過,但沒有成功......

class MentionsLegalesController extends Controller { 

    public function mentionLegalesAction(Request $request) { 

     $em = $this->getDoctrine()->getManager(); 

     $ML = $em->getRepository('Video2LearnBddBundle:MentionLegaleNv1')->findOneById(1); 

     foreach ($ML->getMentionsLegalesNv2() as $tag) { 
      $originalTags->add($tag); 
     } 
     if ($request->getMethod() == 'GET') { 
      $form = $this->createForm(new MentionLegaleNv1Type(), $ML); 
     } 

     if ($request->isMethod('POST')) { 
      $form = $this->createForm(new MentionLegaleNv1Type(), $ML); 
      $form->handleRequest($request); 

      if ($form->isValid()) { 
       foreach ($originalTags as $tag) { 
        if ($ML->getMentionsLegalesNv2()->contains($tag) == false) { 
         $em->persist($tag); 
        } 
       } 

       $em->persist($ML); 
       $em->flush(); 

       return $this->redirect($this->generateUrl('administration_mentions_legales')); 
      } 
     } 

     return $this->render('Video2LearnAdministrationBundle:Pages:mentions_legales.html.twig', array(
        'form' => $form->createView(), 
     )); 
    } 

} 

如何刪除MentionLegaleNv2或MentionLegaleNv3中的項?

感謝您的幫助!

回答

0

我沒有通過其他方式...

我這樣做:

if ($form->isValid()) { 

      // supprime la relation entre le tag et la « Task » 
      foreach ($originalMentionsLegalesNv1 as $mlnv2) { 
       if ($MentionsLegalesNv1->getMentionsLegalesNv2()->contains($mlnv2) == false) { 

        $em->remove($mlnv2); 
       } 
      } 

      $em->persist($MentionsLegalesNv1); 
      $em->flush(); 
0

你刪除的方法應自動更新在你的添加方法不喜歡相同的方式連接的對象..

/** 
* Remove MentionsLegalesNv2 
* 
* @param \Video2Learn\BddBundle\Entity\MentionLegaleNv2 $mentionsLegalesNv2 
*/ 
public function removeMentionsLegalesNv2(MentionLegaleNv2 $mentionsLegalesNv2) { 
    $this->MentionsLegalesNv2->removeElement($mentionsLegalesNv2); 
    $mentionsLegalesNv2->setMentionLegaleNv1(null); 
} 
+0

您好!感謝您的幫助...我嘗試了您的解決方案,但我有這個錯誤消息:「嘗試在/Users/thomasmelcer/Sites/Video2Learn.fr/src類」Video2Learn \ BddBundle \ Entity \ MentionLegaleNv2「上調用方法setMentionLegaleNv1」 /Video2Learn/BddBundle/Entity/MentionLegaleNv1.php第89行。您是否打算調用:「addMentionsLegalesNv3」,「getMentionsLegalesNv1」,「getMentionsLegalesNv3」,「setMentionsLegalesNv1」?「 – Zagloo

+1

哦,沒關係......這是你寫的,但用('s')這樣「$ mentionsLegalesNv2-> setMentionSLegaleSNv1(null);」 因此,解決方案工作但是,我想刪除數據庫中,不只是將其設置爲NULL,因爲它始終存在於數據庫中... – Zagloo

相關問題