Symfony 2和Doctrine。删除表继承的父级

时间:2015-03-03 20:29:59

标签: symfony doctrine-orm doctrine

我有以下结构:

/**
 * @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中引用的通知表中的行" notification_user_notice"被删除但不是父表中的行" notification"。

我尝试使用postRemove侦听器,但由于删除是在表级别进行的,因此未调用它。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

最后,我解决了使用带有PreRemove操作的EventListener:

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;