0

I have a entity that use doctrine extension, tree behaviuor, i found problems in tree and don't know it's reason.

my entity:

MyEntity:
    type:  entity
    gedmo:
        tree:
            type: nested
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
#        ...
        lft:
            type: integer
            gedmo:
                - treeLeft
        rgt:
            type: integer
            gedmo:
                - treeRight
        lvl:
            type: integer
            gedmo:
                - treeLevel
        root:
            type: integer
            gedmo:
                - treeRoot
        createdAt:
            column: created_at
            type: datetime
            gedmo:
                timestampable:
                    on: create
        updatedAt:
            column: updated_at
            type: datetime
            gedmo:
                timestampable:
                    on: update
    oneToMany:
        children:
            targetEntity: MyEntity
            mappedBy: parent
            orderBy:
                lft: ASC
    manyToOne:
        parent:
            targetEntity: MyEntity
            inversedBy: children
            joinColumn:
                name: parent_id
                referencedColumnName: id
                onDelete: "restrict"
            gedmo:
                - treeParent

tree problem:(reported by verify() method)

  0 => "index [8], duplicate on tree root: 1"
  1 => "index [9], duplicate on tree root: 1"
  2 => "index [20], duplicate on tree root: 1"
  3 => "index [21], duplicate on tree root: 1"
  4 => "node [8], left is greater than right on tree root: 1"
  5 => "node [10] left is less than parent`s [7] left value"
  6 => "node [16] right is greater than parent`s [7] right value"
  7 => "node [19] right is greater than parent`s [6] right value"
  8 => "node [20] right is greater than parent`s [6] right value"
  9 => "node [21] right is greater than parent`s [6] right value"
  10 => "node [22] right is greater than parent`s [6] right value"
  11 => "node [23] right is greater than parent`s [6] right value"
  12 => "node [24] right is greater than parent`s [6] right value"
  13 => "node [31] left is less than parent`s [30] left value"
  14 => "node [35] left is less than parent`s [8] left value"
  15 => "node [36] left is less than parent`s [8] left value"

table data

ghanbari
  • 1,630
  • 1
  • 16
  • 31
  • The keys for the elements of the tree are set incorrectly. Check https://en.wikipedia.org/wiki/Nested_set_model to get the concept of this hierarchy model. – Vadim Ashikhman Jun 22 '15 at 09:04
  • @VadimAshikhman can know this model, i dont know why tree have this problems?, i don't let user delete a node that have children and user can only change parent to a node that is not node's children (lower than lft or greater that rgt) – ghanbari Jun 22 '15 at 09:10
  • Its hard to tell why the tree is corrupted without insert/update/delete code snippets. Try to insert few nodes (~= 5) and execute `verify()` method after every insert/update/delete to locate the problem method. – Vadim Ashikhman Jun 22 '15 at 09:14

2 Answers2

1

I've encountered exactly the same issue. Perhaps this answer will help someone else.

Gedmo/Tree with the nested set executes changes in leaf nodes before the deletion transaction begins. Therefore, even the transaction is rollbacked, the updated changes persist. To resolve that you should start the transaction manually before.

Ref. https://github.com/doctrine-extensions/DoctrineExtensions/issues/1062 & https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/transaction-safety.md

lchrusciel
  • 361
  • 1
  • 5
0

I found problem, when i delete a entity by $em->remove method, doctrine extension assume that onDelete=cascade for entity & change lft & rgt values of tree, then run query for removing of entity(and all children), but my entity have onDelete=restrict, then lft & rgt values are updated, but children is not deleted and this raise error in tree

ghanbari
  • 1,630
  • 1
  • 16
  • 31