嘿那里,
我的 sqlite 表中有一些树形结构的数据,想用 php 递归地删除它。数据如下所示:
标识父标识 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 的子类别。execute-command 永远不会完成,相反,它似乎在半分钟内没有做任何事情,之后它开始从从 id 0 开始。
在第二次“尝试”之后,它只返回内部服务器错误(“服务器遇到内部错误或配置错误,无法完成您的请求......”)
如果我用参数 1 调用这个函数,一切正常,因为递归永远不会被调用。
这里发生了什么?
谢谢,
米克