Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sgehrig opened this issue Feb 15, 2023 · 7 comments
Open
Labels

Comments

@sgehrig
Copy link

sgehrig commented Feb 15, 2023

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!

@phansys phansys added the Tree label Feb 20, 2023
@github-actions
Copy link

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 github-actions bot added the Stale label Aug 20, 2023
@sgehrig
Copy link
Author

sgehrig commented Aug 21, 2023

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 github-actions bot removed the Stale label Aug 21, 2023
Copy link

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 github-actions bot added the Stale label Feb 17, 2024
@sgehrig
Copy link
Author

sgehrig commented Feb 17, 2024

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.

@github-actions github-actions bot removed the Stale label Feb 17, 2024
@Hricer
Copy link

Hricer commented Feb 23, 2024

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.

Copy link

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 github-actions bot added the Stale label Aug 21, 2024
@sgehrig
Copy link
Author

sgehrig commented Aug 21, 2024

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.

@github-actions github-actions bot removed the Stale label Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants