那里,
我的sqlite表中有一些树形结构数据,想要用PHP递归删除它。数据如下所示:
Id ParentId 0 -1 1 0
功能:
/**
* Delete a category with all it's subcategories
*
* @param {number} id The id of the category which should be deleted
* @return {number} The number of categories deleted
* @throws {Exception} Throws an exception with some description text if anything goes wrong
*/
public function deleteCategory($id) {
static $catCnt = 0;
Helper::log("deleteCategory: id = $id");
try {
// Open database-connection
$this->openDatabase();
// Remove all subcategories recursively
$stmtSubs = $this->_db->prepare("SELECT Id FROM categories WHERE ParentId = :id");
$stmtSubs->bindParam(":id", $id, PDO::PARAM_INT);
$stmtSubs->execute();
while ($row = $stmtSubs->fetch(PDO::FETCH_ASSOC)) {
Helper::log("deleteCategory: call recursively to delete subcategory with id = " . $row["Id"]);
$this->deleteCategory($row["Id"]);
}
// All subcategories have been removed -> Now it's time to remove the category itself
$stmt = $this->_db->prepare("DELETE FROM Categories WHERE Id = :id");
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
Helper::log("deleteCategory: before execution, id = $id");
$stmt->execute();
// The code from here on never gets executed...
Helper::log("deleteCategory: after execution, id = $id");
$catCnt += $stmt->rowCount();
return $catCnt;
} catch (Exception $ex) {
throw new Exception("Deletion of category failed:<br>" . $ex->getMessage());
}
}
如果我用参数0调用这个函数,我得到这个输出:
31.08.2012 09:39:58: deleteCategory: id = 0
31.08.2012 09:39:58: deleteCategory: call recursively to delete subcategory with id = 1
31.08.2012 09:39:58: deleteCategory: id = 1
31.08.2012 09:39:58: deleteCategory: before execution, id = 1
// Here should be the "after execution"-entry, but instead it "waits" half a minute and starts the whole execution from the beginning...
31.08.2012 09:40:28: deleteCategory: id = 0
31.08.2012 09:40:44: deleteCategory: call recursively to delete subcategory with id = 1
31.08.2012 09:40:44: deleteCategory: id = 1
31.08.2012 09:40:44: deleteCategory: before execution, id = 1
正如你所看到它执行得很好,直到它实际上应该删除id为1的子类别.cute-command永远不会完成,相反它似乎没有做任何事情半分钟,然后它开始整个从id开始的递归。
在第二次“尝试”之后,它只返回内部服务器错误(“服务器遇到内部错误或配置错误,无法完成您的请求......”)
如果我用参数1调用此函数,一切正常,因为永远不会调用递归。
这里发生了什么?
谢谢,
Mik的
答案 0 :(得分:0)
挣扎了半天,决定将它贴在这里,贴在这里,发布后再看一遍,看到了问题:
“$ this-&gt; openDatabase();”的递归调用是所有邪恶的根源......
谢谢;)