类表继承中的级联删除不会删除父行



我有一个名为Notification的父类,它的子类之一是CommentNotification(类表继承(。

/**
* This entity represents the notifications that are sent to users when an event happens
* @ORMEntity(repositoryClass="AppBundleRepositoryNotificationRepository")
* @ORMInheritanceType("JOINED")
* @ORMDiscriminatorColumn(name="type", type="string")
* @ORMDiscriminatorMap({
*     "yp" = "YpNotification",
*     "default" = "Notification",
*     "comment" = "CommentNotification",
*     "post" = "PostNotification"})
* @ORMTable(name="notification")
*/
class Notification
{
/**
* The identifier of this notification
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @var int $id
*/
protected $id;
}

CommentNotification中,我包含了onDelete = "CASCADE",因此当删除注释时,附加在其上的通知也会被删除。

/**
* @ORMEntity
* @ORMTable(name="comment_notification")
* @ORMEntity(repositoryClass="AppBundleEntityNotificationsCommentNotificationRepository")
*/  
class CommentNotification extends Notification
{
/**
*
* @ORMManyToOne(targetEntity="AppBundleEntityContentItemContentItemComment")
* @ORMJoinColumn(name="comment_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
private $comment;
...
}

应要求,我也显示ContentItemComment。这不包含与CommentNotification的双向关系。

/**
* 
* @ORMTable(name="content_item_comment")
* @ORMEntity(repositoryClass="AppBundleEntityContentItemContentItemCommentRepository")
*/
class ContentItemComment
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
...
}

然而,它成功地删除了comment_notification中的行,但notification中的行仍然存在,在通知表中留下了每次都必须手动删除的重影记录。

F.e这个查询每天都会返回一些新结果:

SELECT * FROM `notification` n WHERE n.id not in (select id from comment_notification) and n.type='comment' 

我是否遗漏了Notification中的注释?

条令网站上有以下注释:

如果不使用SchemaTool生成所需的SQL,则应该知道删除类表继承会在所有数据库实现中使用外键属性ON DELETE CASCADE。如果您自己未能实现这一点,将导致数据库中出现死行。

这可能是原因吗?

因此,正如注释中提到的那样-如果它是双向关系-则需要将orphanRemoval=true选项添加到相关的OneToMany属性中。

最新更新