Class Table 中的级联删除继承不删除父行
Cascading Delete in Class Table Inheritance doesn't delete parent row
我有一个名为 Notification
的父项 class,它的子项之一是 CommentNotification
(Class table 继承)。
/**
* This entity represents the notifications that are sent to users when an event happens
* @ORM\Entity(repositoryClass="AppBundle\Repository\NotificationRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({
* "yp" = "YpNotification",
* "default" = "Notification",
* "comment" = "CommentNotification",
* "post" = "PostNotification"})
* @ORM\Table(name="notification")
*/
class Notification
{
/**
* The identifier of this notification
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @var int $id
*/
protected $id;
}
在 CommentNotification
中,我包含了 onDelete = "CASCADE"
,这样当评论被删除时,附在评论上的通知也会被删除。
/**
* @ORM\Entity
* @ORM\Table(name="comment_notification")
* @ORM\Entity(repositoryClass="AppBundle\Entity\Notifications\CommentNotificationRepository")
*/
class CommentNotification extends Notification
{
/**
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\ContentItem\ContentItemComment")
* @ORM\JoinColumn(name="comment_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
private $comment;
...
}
根据要求,我也显示了 ContentItemComment。这不包含与 CommentNotification 的双向关系。
/**
*
* @ORM\Table(name="content_item_comment")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ContentItem\ContentItemCommentRepository")
*/
class ContentItemComment
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
}
但是它成功删除了 comment_notification
中的行,但是 notification
中的行仍然存在,在通知 table 中给我留下了幻影记录,我必须手动删除每个时间。
F.e 这个查询每天都会 return 一些新的结果:
SELECT * FROM `notification` n WHERE n.id not in (select id from comment_notification) and n.type='comment'
我是否遗漏了 Notification
中的注释?
Doctrine site 上有以下注释:
When you do not use the SchemaTool to generate the required SQL you should know that deleting a class table inheritance makes use of the foreign key property ON DELETE CASCADE in all database implementations. A failure to implement this yourself will lead to dead rows in the database.
可能是这个原因吗?
所以正如评论中提到的那样 - 如果它是双向关系 - 您需要将 orphanRemoval=true
选项添加到相关的 OneToMany
属性.
我有一个名为 Notification
的父项 class,它的子项之一是 CommentNotification
(Class table 继承)。
/**
* This entity represents the notifications that are sent to users when an event happens
* @ORM\Entity(repositoryClass="AppBundle\Repository\NotificationRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({
* "yp" = "YpNotification",
* "default" = "Notification",
* "comment" = "CommentNotification",
* "post" = "PostNotification"})
* @ORM\Table(name="notification")
*/
class Notification
{
/**
* The identifier of this notification
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @var int $id
*/
protected $id;
}
在 CommentNotification
中,我包含了 onDelete = "CASCADE"
,这样当评论被删除时,附在评论上的通知也会被删除。
/**
* @ORM\Entity
* @ORM\Table(name="comment_notification")
* @ORM\Entity(repositoryClass="AppBundle\Entity\Notifications\CommentNotificationRepository")
*/
class CommentNotification extends Notification
{
/**
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\ContentItem\ContentItemComment")
* @ORM\JoinColumn(name="comment_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
private $comment;
...
}
根据要求,我也显示了 ContentItemComment。这不包含与 CommentNotification 的双向关系。
/**
*
* @ORM\Table(name="content_item_comment")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ContentItem\ContentItemCommentRepository")
*/
class ContentItemComment
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
}
但是它成功删除了 comment_notification
中的行,但是 notification
中的行仍然存在,在通知 table 中给我留下了幻影记录,我必须手动删除每个时间。
F.e 这个查询每天都会 return 一些新的结果:
SELECT * FROM `notification` n WHERE n.id not in (select id from comment_notification) and n.type='comment'
我是否遗漏了 Notification
中的注释?
Doctrine site 上有以下注释:
When you do not use the SchemaTool to generate the required SQL you should know that deleting a class table inheritance makes use of the foreign key property ON DELETE CASCADE in all database implementations. A failure to implement this yourself will lead to dead rows in the database.
可能是这个原因吗?
所以正如评论中提到的那样 - 如果它是双向关系 - 您需要将 orphanRemoval=true
选项添加到相关的 OneToMany
属性.