使用导入脚本更改父级时,Gedmo树设置了错误的lft和rgt值

时间:2016-08-29 14:53:46

标签: php symfony tree nested

我已经使用Dedmo NestedTree设置了一个标准类别实体,并且我编写了一个脚本来导入与其父级相关的类别列表。

类别设置得很好,父母设置得很好,但是lft和rgt值是错误的。

由于我可以在循环中添加很多类别,我不认为搜索父级并在单循环中执行刷新是最好的解决方案...

什么可能导致lft,rgt值出现此错误?这是我的代码中的错误还是GedmoTree中的错误(限制)?

/**
@ORM\Entity(repositoryClass="MyVendor\MyBundle\Repository\CategoryRepository")
 * @Gedmo\Tree(type="nested")
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * name of the category.
     *
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(name="root", type="integer", nullable=true)
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(
     *     targetEntity="MyVendor\MyBundle\Entity\Category",
     *     inversedBy="children"
     * )
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $parent;

    /**
     * @ORM\ManyToOne(targetEntity="MyVendor\MyBundle\Entity\Poll")
     * @ORM\JoinColumn(name="poll_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $poll;

    /**
     * @ORM\OneToMany(
     *     targetEntity="MyVendor\MyBundle\Entity\Category",
     *     mappedBy="parent",
     * )
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    protected $children;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }
...
}

然后我设置一个脚本来导入具有层次结构的类别列表:

这是csv文件:

CategoyName; ParentCategory

RootCategory;null
Category1;RootCategory
Category2;RootCategory
Category3;RootCategory
Category3-1;Category3
Category3-1-1;Category3-1

脚本:

public function importCategories(Poll $poll, array $categories)
{
    $cats = [];
    $parents = [];
    $children = [];

    $repoCat = $this->om->getRepository('MyVendorMyBundle:Category');

    foreach ($categories as $category) {
        if (count($category) > 1) {
            $categoryName = $category[0];
            $categoryParent = $category[1];
            //does the category exists already ?
            $cat = $repoCat->findOneBy([
                'name' => $categoryName,
                'poll' => $poll,
            ]);
            //if it doesn't
            if ($cat === null) {
                //create category, with no parent
                $newCategory = new Category();
                $newCategory->setName($categoryName);
                $newCategory->setParent(null);
                $newCategory->setPoll($poll);
                $newCategory->setUser($user);
                $this->om->persist($newCategory);

                //save infos
                $cats[] = $newCategory;
                $children[] = $categoryName;
                $parents[] = $categoryParent;
            }
        }
    }
    $this->om->flush();

    //update parent
    //search for parent
    foreach ($parents as $id => $parent) {
        if ($parent !== 'null') {
            $pc = $repoCat->findOneBy([
                'name' => $parent,
                'poll' => $poll,
            ]);
            //if parent is created
            if ($pc !== null) {
                $cats[$id]->setParent($pc);
                $this->om->persist($cats[$id]);
                $this->om->flush();
            }
        }
    }
    $this->om->flush();
}

当我运行此脚本时,我有这样的结果:

id | parent | name | left | lvl | rgt
161 | NULL | RootCategory | 1 | 0 | 12
162 | 161 | Category1 | 10 | 1 | 11
163 | 161 | Category2 | 8 | 1 | 9
164 | 161 | Category3 | 2 | 1 | 7
165 | 164 | Category3-1 | 3 | 2 | 6
166 | 165 | Category3-1-1 | 4 | 3 | 5

而不是

id | parent | name | left | lvl | rgt
161 | NULL | RootCategory | 1 | 0 | 12
162 | 161 | Category1 | 2 | 1 | 3
163 | 161 | Category2 | 4 | 1 | 5
164 | 161 | Category3 | 6 | 1 | 11
165 | 164 | Category3-1 | 7 | 2 | 10
166 | 165 | Category3-1-1 | 8 | 3 | 9

我看到了this kind of post,我试图添加

$repo->verify();
$repo->recover();
在冲洗之前

,但它没有改变任何东西

0 个答案:

没有答案