4

I am trying to create Forum, which lists its Topics in a tree structure. I am using following Doctrine 2 (on mysql) entity to store Topic info.

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

Every Topic has its own id and can be child of another one. If it is, it is stored in parentTopic_id. It works, but whenever I try to delete 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 to add onDelete="cascade" to row with "joinColumn", but it didn't help. How to fix it, so I can delete any topic?

EDIT: Sorry for delay in responses, I had christmas to celebrate and stuff :-). In this screen you can see data in the Table as requested. enter image description here

**EDIT 2: ** Yes, it was a circular reference.

Kudlas
  • 659
  • 7
  • 24
  • Have you already tried `cascade={"remove"}` in the association as described [here](https://stackoverflow.com/a/6334710/2270041)? – Matteo Dec 18 '17 at 16:59
  • 1
    Yes, it did not work. – Kudlas Dec 18 '17 at 17:00
  • What happens if you print out the generated SQL and try it in DB? It works? Can you elaborate more the question? I am not able to follow you on what do you want! – ReynierPM Dec 18 '17 at 19:15
  • If I am not wrong you're trying to delete a topic with some children and you can't do that unless you set the value for the `parentTopicId` as `NULL` or something else. – ReynierPM Dec 18 '17 at 19:21
  • 1
    Seems to me like you have a Comment (forum_comment) entity which has relation to topic and you do not cascade comments. – Laurynas Dec 18 '17 at 20:47
  • Show us the contents of the row in question; it sounds like you have a circular reference. – Rick James Dec 22 '17 at 16:27

0 Answers0