doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4k stars 1.26k forks source link

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

Open sgehrig opened 1 year ago

sgehrig commented 1 year ago

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!

github-actions[bot] commented 10 months ago

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.

sgehrig commented 10 months ago

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?

github-actions[bot] commented 4 months ago

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.

sgehrig commented 4 months ago

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.

Hricer commented 4 months ago

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.