如何恢复根节点

问题描述 投票:0回答:2

使用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

它成功恢复了左右和水平值,但没有恢复根。 我无法手动设置根值(受教义侦听器禁止)。

如何更新这些根值以便树可以正常工作?

谢谢!

php symfony doctrine-orm tree nested
2个回答
0
投票

如果您有多个根节点和多个子类别,您可以运行此查询:

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();

数据集示例:

  • 餐厅(id:1,树根:1)
    • 披萨(id:2,树根:1)
    • 汉堡(id:3,树根:1)
  • bar(id:4,树根,4)
    • 鸡尾酒吧(id:5,树根:4)
    • dj 酒吧(id:6,树根:4)
  • 商店(id:7,树根:7)
    • 面包店(id:8,树根:7)
    • 冰淇淋(id:9,树根:7)
    • 酒铺(id:10,树根:7)

-1
投票

嗯,我没有找到面向对象的解决方案,所以我选择了原始 SQL 查询。 我在任何实体获得父级之前执行此操作,因此每个实体的根值应等于其自己的 id。

在 phpmyadmin 中,粘贴以下 sql 查询并执行:

update recipe
set recipe.root = recipe.id
© www.soinside.com 2019 - 2024. All rights reserved.