3

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?

IT-Girl
  • 448
  • 5
  • 24
  • 3
    Hi, can you show `ContentItemComment` entity too? Is it bi-directional? If it is try to use `mappedBy="comment", cascade={"remove"}, orphanRemoval=true)` in its `OneToMany` relation – Evgeny Ruban Apr 08 '20 at 12:57
  • @EugeneRuban. Adding orphanRemoval = true fixed my problem! i did not expect this as I thought adding it would only delete 'CommentNotification' and not 'Notification'. Can you add this as an answer, so I can accept it? – IT-Girl May 10 '20 at 07:56
  • Glad to be helpful. Added the answer. – Evgeny Ruban May 10 '20 at 14:21
  • @EugeneRuban I accepted it. Thanks so much :) – IT-Girl May 11 '20 at 13:47

2 Answers2

2

On the Doctrine site is the following note:

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.

Is this the possible reason?

craigh
  • 1,909
  • 1
  • 11
  • 24
1

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.

Evgeny Ruban
  • 1,357
  • 1
  • 14
  • 20