5

I'm trying to replicate example from gedmo nested-set extension blog, where there are many parent nodes. There you are able to create as many movable parent nodes as well as children (which is typical for a nested set­Wikipedia).

Reading trough comment section, common advice is removing @Gedmo\TreeRoot annotation/mapping, but if I do that, I'm able to move root nodes, but tree becomes broken particularly left and right id's. If I keep TreeRoot, and try to move root nodes I get "no node siblings" or something along those lines, as expected.

Looking at live example at extension blog you can see that you are able to create category without parent and move it up or down.

My Category entity - relevant parts:

class Category 
{
    /**
     * @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="Category", inversedBy="children")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     * @Exclude
     */
    private $children;
}

Question: How to make multiple root tree (I would like to avoid creating fake category that will be single root, and add everything else as child of that node), and be able to move root nodes up and down?

P.S. I'm on master branch.

hakre
  • 193,403
  • 52
  • 435
  • 836
Marko Jovanović
  • 2,647
  • 3
  • 27
  • 36
  • @lopsided At the end I've done what I wanted to avoid. Adding "fake" tree root category and all other root nodes as children of the fake one. It works, although there are some downsides, hiding the fake category when displaying categories (in my case) in forms or when rendering them inside unordered list etc. – Marko Jovanović Jul 12 '12 at 09:37
  • Thats what Ive gone with now too. Works ok for me, but I do keep breaking the tree, not sure it is totally robust. – lopsided Jul 12 '12 at 18:36

3 Answers3

1

The tree will break on concurrent updates see tree locking documentation. When tree updates it runs two atomic update queries, which in turn can be run from a concurrent request with a different state not aware about ones being done. When having this covered, a tree should maintain its stability, I have successfully used this extension for 100K - 1M node tree, which was based on roots though.

If you are sure these tree brakes are not related to concurrency, then please open an issue on github with an use case description.

Gediminas
  • 868
  • 7
  • 11
0

Your solution to create a fake root Category is the best in my point of view. Then your real roots will be the children of this fake root.

To display the full tree sorted by name, just use the repository function reorder on your fake root node.

Lucius9
  • 11
-3

You could swap the ids of the root nodes to reorder them.