使用symfony2和doctrine2以及条令树扩展,我最近更新了一个实体,使其成为条令嵌套集树。
学说模式更新强制添加了带有空数据的正确列。
然后我运行了以下代码:
$repo = $this->getDoctrine()->getRepository('AppBundle:FoodAnalytics\Recipe');
$repo->verify();
// can return TRUE if tree is valid, or array of errors found on tree
$repo->recover();
$this->flush(); // important: flush recovered nodes
// if tree has errors it will try to fix all tree
它成功恢复了左右和水平值,但没有恢复根。 我无法手动设置根值(受教义侦听器禁止)。
如何更新这些根值以便树可以正常工作?
谢谢!
如果您有多个根节点和多个子类别,您可以运行此查询:
UPDATE category SET category.tree_root = IF(category.parent_id IS NULL, category.id, category.parent_id)
只有在没有父级(因此它是根类别)时,才会将类别 id 设置为树节点,否则会将父级 id 设置为树 id。
然后修复,运行:
$em->clear();
$repository->verify();
$repository->recover();
$em->flush();
数据集示例:
嗯,我没有找到面向对象的解决方案,所以我选择了原始 SQL 查询。 我在任何实体获得父级之前执行此操作,因此每个实体的根值应等于其自己的 id。
在 phpmyadmin 中,粘贴以下 sql 查询并执行:
update recipe
set recipe.root = recipe.id