2017-05-15 196 views
0

我有以下實體doctrine2 - 刪除所有一對多關係

  1. 專業
class Professional extends User 
{ 
    /** 
    * @ORM\OneToMany(targetEntity="UserBundle\Entity\Timeslot", mappedBy="professional", cascade={"persist"}) 
    */ 
    protected $timeslots; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->timeslots = new ArrayCollection(); 
    } 

    /** 
    * Add timeslot 
    * 
    * @param \UserBundle\Entity\Timeslot $timeslot 
    * 
    * @return Professional 
    */ 
    public function addTimeslot(\UserBundle\Entity\Timeslot $timeslot) 
    { 
     $this->timeslots[] = $timeslot; 

     return $this; 
    } 

    /** 
    * Remove timeslot 
    * 
    * @param \UserBundle\Entity\Timeslot $timeslot 
    */ 
    public function removeTimeslot(\UserBundle\Entity\Timeslot $timeslot) 
    { 
     $this->timeslots->removeElement($timeslot); 
    } 

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

    public function clearTimeslots() 
    { 
     $this->timeslots->clear(); 
    } 
} 
  • 時隙實體
  • class Timeslot 
    { 
        /** 
        * @ORM\Column(type="integer") 
        * @ORM\Id 
        * @ORM\GeneratedValue(strategy="AUTO") 
        */ 
        private $id; 
    
        /** 
        * @ORM\Column(name="day", type="string", length=20, nullable=false) 
        */ 
        private $day; 
    
        /** 
        * @ORM\Column(name="starttime", type="time", nullable=true) 
        */ 
        private $startTime; 
    
        /** 
        * @ORM\Column(name="endtime", type="time", nullable=true) 
        */ 
        private $endTime; 
    
        /** 
        * @ORM\Column(name="available", type="boolean", nullable=false) 
        */ 
        private $available = true; 
    
        /** 
        * @ORM\ManyToOne(targetEntity="UserBundle\Entity\Professional", inversedBy="timeslots", cascade={"persist"}) 
        * @ORM\JoinColumn(name="professional_id", referencedColumnName="id", unique=false, nullable=false) 
        */ 
        private $professional; 
    } 
    

    我想刪除所有時隙對於給定的專業,我試圖做

    $professional->getTimeslots()->clear(); 
    $em->persist($professional); 
    $em->flush(); 
    

    這不會刪除數據,我怎麼刪除所有時隙對於給定的專業?

    +0

    我推薦一個允許它的'Repository'函數。在你的情況下,你不清除關係中擁有的一方。 – LBA

    +0

    需要級聯刪除您的Professional :: timeSlots屬性。 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations級聯的東西可能會很棘手,至少可以說。在Timeslot :: professional上擁有級聯屬性沒什麼意義。 – Cerad

    回答

    2

    您可以通過->clear()實現此目的,但是您必須向Professional實體添加一些代碼。

    @ORM\OneToMany(targetEntity="UserBundle\Entity\Timeslot", mappedBy="professional", cascade={"merge", "persist"}, orphanRemoval=true) 
    
    1. 添加級聯= 「合併」
    2. 設置orphanRemoval =真

    然後在您的控制器,你可以:

    $professional->getTimeslots()->clear(); 
    $professional = $em->merge($professional); 
    $em->flush(); 
    

    希望它可以幫助