2017-05-15 2 views
0

Ich habe folgende EinheitenDoctrine2 - löschen alle OneToMany Beziehungen

  1. Professionelle
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(); 
    } 
} 
  1. Zeitfenster Entity
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; 
} 

Ich möchte alle Zeitfenster für ein bestimmten Beruf löschen, habe ich versucht zu tun

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

Dies löscht nicht die Daten, wie lösche ich alle Zeitfenster für einen bestimmten Profi?

+0

hilft Ich empfehle eine "Repository" -Funktion, die es erlaubt. In deinem Fall räumst du * die * Eigentümerseite * der Beziehung nicht auf. – LBA

+0

Sie benötigen eine Cascade-Entfernung für Ihre Professional :: timeSlots-Eigenschaft. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations Cascade Zeug kann gelinde gesagt schwierig sein. Eine Kaskadeneigenschaft auf Timeslot :: Professional zu haben macht wenig Sinn. – Cerad

Antwort

2

Sie können dies mit ->clear() erreichen, aber Sie müssen Code zu Ihrer Professional Einheit hinzufügen.

@ORM\OneToMany(targetEntity="UserBundle\Entity\Timeslot", mappedBy="professional", cascade={"merge", "persist"}, orphanRemoval=true) 
  1. Hinzufügen einer Kaskade = "merge"
  2. Set orphanRemoval = true

in Ihrem Controller Dann können Sie einfach:

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

Hoffe, dass es

Verwandte Themen