Skip to content

[Tree][NestedSet] Invalid tree when multiple roots are inserted in one flushΒ #2582

@sgehrig

Description

@sgehrig

Using the following simple Tree

#[ORM\Entity(repositoryClass: OURepository::class)]
#[ORM\Table(name: 'ous')]
#[ORM\Index(columns: ['lft', 'rgt'], name: 'idx_tree')]
#[Gedmo\Tree(type: 'nested')]
class OU
{
    #[ORM\Id]
    #[ORM\Column('id', 'guid')]
    private string $id;

    #[ORM\ManyToOne(targetEntity: OU::class, inversedBy: 'children')]
    #[ORM\JoinColumn(name: 'parent', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Gedmo\TreeParent]
    private ?self $parent = null;

    #[ORM\Column(name: 'lft', type: 'integer', options: ['unsigned' => true])]
    #[Gedmo\TreeLeft]
    private int $left = 1;

    #[ORM\Column(name: 'lvl', type: 'integer', options: ['unsigned' => true])]
    #[Gedmo\TreeLevel]
    private int $level = 0;

    #[ORM\Column(name: 'rgt', type: 'integer', options: ['unsigned' => true])]
    #[Gedmo\TreeRight]
    private int $right = 2;

    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: OU::class)]
    #[ORM\OrderBy(['left' => 'ASC'])]
    private Collection $children;

    public function __construct(string $id, ?self $parent = null)
    {
        $this->id = $id;
        $this->children = new ArrayCollection();
        $this->parent = $parent;
        if ($parent) {
            $parent->children->add($this);
        }
    }
}

I create multiple roots in one flush():

$ou1 = new OU('id1', null);
$ou11 = new OU('id11, $ou1);
$ou2 = new OU('id2', null);
$ou21 = new OU('id21, $ou2);

Now there's a corrupt tree in the database:

id parent lft lvl rgt
id1 1 0 8
id11 id1 2 1 7
id2 3 0 6
id21 id2 4 1 5

If I split the two roots into two separate flush()s, the tree is valid:

id parent lft lvl rgt
id1 1 0 4
id11 id1 2 1 3
id2 5 0 8
id21 id2 6 1 7

Did I miss something or is this indeed a bug?

Thanks a lot for your support and your great work! Much appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions