Why can't I delete a row attached to it using Doctrine 2

I am trying to create a Forum that lists its Topics in a tree structure. I use the following Doctrine 2 (for mysql) to store topic information.

/**
 * @ORM\Entity
 */
class ForumTopic extends \Kdyby\Doctrine\Entities\BaseEntity
{


    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    public $id;

...

    /**
     * @ORM\ManyToOne(targetEntity="ForumTopic")
     * @ORM\JoinColumn(name="ForumTopic", referencedColumnName="id", nullable=true)
     * @var string
     */
    public $parentTopic_id;

Each topic has its own identifier and can be a child. If so, it is stored in parentTopic_id. It works, but whenever I try to delete a topic, this error appears.

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`sandbox`.`forum_comment`, CONSTRAINT `FK_65B81F1D8D182203` FOREIGN KEY (`ForumTopic`) REFERENCES `forum_topic` (`id`))

I tried adding onDelete = "cascade" to the line with "joinColumn", but that didn't help. How to fix it, so I can delete any topic?

EDIT: Sorry for the delay in answering, I had Christmas to celebrate and stuff :-). On this screen you can see the data in the table in accordance with the requests. enter image description here

+4

Source: https://habr.com/ru/post/1690860/


All Articles