Reputation: 2869
How to also set $isSuspended
to true
when Teacher is removed ?
<?php
class Teacher {
/**
*@ORM\OneToMany(targetEntity="Activities", mappedBy="teacher")
*/
protected $activities;
}
class Activities {
/**
* @ORM\ManyToOne(targetEntity="Teacher", inversedBy="activities")
* @ORM\JoinColumn(name="teacher_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
protected $teacher;
/**
* Activities are suspended when there is not teacher
*/
protected $isSuspended = false;
}
Upvotes: 0
Views: 387
Reputation: 2561
This part onDelete="SET NULL"
works on a database level, so for changing the $isSuspended
field, you can create a listener for the postRemove
doctrine event.
use Doctrine\ORM\Event\LifecycleEventArgs;
public function postRemove(LifecycleEventArgs $event)
{
$entity = $event->getEntity();
if ($entity instanceof Teacher) {
foreach($entity->getActivities() as $activity) {
$activity->setIsSuspended(true);
$event->getEntityManager()->persist($activity);
}
$event->getEntityManager()->flush();
}
}
Upvotes: 1