symfonyannotationsdoctrineclass-table-inheritance

Cascading Delete in Class Table Inheritance doesn't delete parent row


I have a parent class called Notification, that has CommentNotification as one of it's children (Class table inheritance).

/**
 * 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;
}

In CommentNotification, I have included onDelete = "CASCADE", so that when the comment is deleted the notification that was attached to it also is deleted.

/**
     * @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;
    ...
}

On request I show ContentItemComment too. This does not contain a bidirectional relationship with 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;
...
}

However it succesfully deletes the row in comment_notification, but the row in notification still exists, leaving me with ghost records in the notification table that I manually have to delete every time.

F.e this query will return some new results everyday:

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

Did I miss an annotation in Notification?


Solution

  • so as it was mentioned in comments - if it is a bi-directional relation - you need to add orphanRemoval=true option to related OneToMany property.