Symfony 2 和学说。删除 table 继承的父项
Symfony 2 and Doctrine. Delete parent on table inheritance
我有以下结构:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"notice" = "UserNoticeNotification", "change_shift" = "ChangeShiftRequestNotification", "change_watch" = "ChangeWatchRequestNotification"})
*/
class Notification {
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="notification_user_notice")
*/
class UserNoticeNotification extends Notification {
/**
* @ORM\ManyToOne(targetEntity="Intranet\WebBundle\Entity\Notice",cascade={"remove"})
* @ORM\JoinColumn(name="notice_id", referencedColumnName="id",onDelete="CASCADE")
**/
protected $notice;
我的问题是,当我删除 UserNoticeNotification 中引用的通知时,table“notification_user_notice
”中的行被删除,但父 table“[=13”中的行未被删除=]”。
我尝试使用 postRemove
侦听器,但它没有被调用,因为删除是在 table 级别进行的。
有什么想法吗?
谢谢
最后我使用带有 PreRemove 操作的事件侦听器解决了这个问题:
public function preRemove(LifecycleEventArgs $args) {
$entity = $args->getEntity();
$em = $args->getEntityManager();
if (get_class($entity) == 'Intranet\WebBundle\Entity\Notice') {
$notificationRepository = $em->getRepository('IntranetNotificationsBundle:UserNoticeNotification');
$notifications = $notificationRepository->findByNotice($entity);
foreach ($notifications as $notification) {
$em->remove($notification);
}
$em->flush();
}
}
并像这样定义实体:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="notification_user_notice")
*/
class UserNoticeNotification extends Notification {
/**
* @ORM\ManyToOne(targetEntity="Intranet\WebBundle\Entity\Notice")
* @ORM\JoinColumn(name="notice_id", referencedColumnName="id")
**/
protected $notice;
我有以下结构:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"notice" = "UserNoticeNotification", "change_shift" = "ChangeShiftRequestNotification", "change_watch" = "ChangeWatchRequestNotification"})
*/
class Notification {
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="notification_user_notice")
*/
class UserNoticeNotification extends Notification {
/**
* @ORM\ManyToOne(targetEntity="Intranet\WebBundle\Entity\Notice",cascade={"remove"})
* @ORM\JoinColumn(name="notice_id", referencedColumnName="id",onDelete="CASCADE")
**/
protected $notice;
我的问题是,当我删除 UserNoticeNotification 中引用的通知时,table“notification_user_notice
”中的行被删除,但父 table“[=13”中的行未被删除=]”。
我尝试使用 postRemove
侦听器,但它没有被调用,因为删除是在 table 级别进行的。
有什么想法吗?
谢谢
最后我使用带有 PreRemove 操作的事件侦听器解决了这个问题:
public function preRemove(LifecycleEventArgs $args) {
$entity = $args->getEntity();
$em = $args->getEntityManager();
if (get_class($entity) == 'Intranet\WebBundle\Entity\Notice') {
$notificationRepository = $em->getRepository('IntranetNotificationsBundle:UserNoticeNotification');
$notifications = $notificationRepository->findByNotice($entity);
foreach ($notifications as $notification) {
$em->remove($notification);
}
$em->flush();
}
}
并像这样定义实体:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="notification_user_notice")
*/
class UserNoticeNotification extends Notification {
/**
* @ORM\ManyToOne(targetEntity="Intranet\WebBundle\Entity\Notice")
* @ORM\JoinColumn(name="notice_id", referencedColumnName="id")
**/
protected $notice;