为什么树无效?
Why tree is invalid?
我有一个使用 doctrine extension, tree behaviuor 的实体,我在树中发现了问题,但不知道是什么原因。
我的实体:
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
树问题:(由verify()方法报告)
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"
我发现了问题,当我通过 $em->remove
方法删除实体时,学说扩展假定 onDelete=cascade
用于实体并更改树的 lft
和 rgt
值,然后 运行 查询删除实体(和所有 children),但我的实体有 onDelete=restrict
,然后更新 lft
和 rgt
值,但 children 未被删除,这在树
中引发错误
我遇到了完全相同的问题。也许这个答案会对其他人有所帮助。
Gedmo/Tree with the nested set 在删除事务开始之前执行叶节点中的更改。因此,即使事务被回滚,更新的更改仍然存在。要解决这个问题,您应该先手动启动交易。
参考。 https://github.com/doctrine-extensions/DoctrineExtensions/issues/1062 & https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/transaction-safety.md
我有一个使用 doctrine extension, tree behaviuor 的实体,我在树中发现了问题,但不知道是什么原因。
我的实体:
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
树问题:(由verify()方法报告)
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"
我发现了问题,当我通过 $em->remove
方法删除实体时,学说扩展假定 onDelete=cascade
用于实体并更改树的 lft
和 rgt
值,然后 运行 查询删除实体(和所有 children),但我的实体有 onDelete=restrict
,然后更新 lft
和 rgt
值,但 children 未被删除,这在树
我遇到了完全相同的问题。也许这个答案会对其他人有所帮助。
Gedmo/Tree with the nested set 在删除事务开始之前执行叶节点中的更改。因此,即使事务被回滚,更新的更改仍然存在。要解决这个问题,您应该先手动启动交易。
参考。 https://github.com/doctrine-extensions/DoctrineExtensions/issues/1062 & https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/transaction-safety.md