0
我有以下實體doctrine2 - 刪除所有一對多關係
- 專業
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();
這不會刪除數據,我怎麼刪除所有時隙對於給定的專業?
我推薦一個允許它的'Repository'函數。在你的情況下,你不清除關係中擁有的一方。 – LBA
需要級聯刪除您的Professional :: timeSlots屬性。 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations級聯的東西可能會很棘手,至少可以說。在Timeslot :: professional上擁有級聯屬性沒什麼意義。 – Cerad