0
Ich habe folgende EinheitenDoctrine2 - löschen alle OneToMany Beziehungen
- 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();
}
}
- 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?
hilft Ich empfehle eine "Repository" -Funktion, die es erlaubt. In deinem Fall räumst du * die * Eigentümerseite * der Beziehung nicht auf. – LBA
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