DoctrineExtensions icon indicating copy to clipboard operation
DoctrineExtensions copied to clipboard

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

Open sgehrig opened this issue 2 years ago • 7 comments

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!

sgehrig avatar Feb 15 '23 12:02 sgehrig

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Aug 20 '23 09:08 github-actions[bot]

Anything new on this one? Am I the only one experiencing this one? Is there something wrong with my setup? Or even with my assumption on how this should work?

sgehrig avatar Aug 21 '23 04:08 sgehrig

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Feb 17 '24 10:02 github-actions[bot]

Still an issue. I'd love to help fixing but I need some hint on where to start if possible. Any help is much appreciated.

sgehrig avatar Feb 17 '24 11:02 sgehrig

I have the same issue. It works correctly if you persist all root nodes first.

$this->doctrine->persist($ou1);
$this->doctrine->persist($ou2);
$this->doctrine->persist($ou11);
$this->doctrine->persist($ou21);

$this->doctrine->flush();

Why does the order matter? I think this is a critical issue. It is not possible use cascade persist.

Hricer avatar Feb 23 '24 14:02 Hricer

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Aug 21 '24 15:08 github-actions[bot]

I think this is still a valid issue. Perhaps somebody could have a look if possible. I'd try to provide a fix as well but I need some guidance to begin with.

sgehrig avatar Aug 21 '24 15:08 sgehrig