PHP recursive function to delete all child nodes causes stackoverflow

前端 未结 2 1377
深忆病人
深忆病人 2021-02-15 18:47

My MySQL looks like this: (the name of the table is category)

\'id\', \'content\', \'parent\'

where:

  • id = the id of the category<
2条回答
  •  名媛妹妹
    2021-02-15 19:36

    Alternatively, you could let the database handle this. In MySQL, an InnoDB ON DELETE CASCADE will do this automatically.

    CREATE TABLE category (
        id INT PRIMARY KEY AUTO_INCREMENT,
        parent_id INT NULL,
        FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE CASCADE
    ) ENGINE=InnoDB
    

    Root nodes should have NULL as parent (not 0 as some people seem to employ on Adjancency List tables).

提交回复
热议问题