Nested.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. namespace Gedmo\Tree\Strategy\ORM;
  3. use Gedmo\Exception\UnexpectedValueException;
  4. use Doctrine\ORM\Proxy\Proxy;
  5. use Gedmo\Tool\Wrapper\EntityWrapper;
  6. use Gedmo\Tool\Wrapper\AbstractWrapper;
  7. use Gedmo\Tree\Strategy;
  8. use Doctrine\ORM\EntityManager;
  9. use Gedmo\Tree\TreeListener;
  10. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  11. use Doctrine\ORM\Query;
  12. use Gedmo\Mapping\Event\AdapterInterface;
  13. /**
  14. * This strategy makes the tree act like a nested set.
  15. *
  16. * This behavior can impact the performance of your application
  17. * since nested set trees are slow on inserts and updates.
  18. *
  19. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. class Nested implements Strategy
  23. {
  24. /**
  25. * Previous sibling position
  26. */
  27. const PREV_SIBLING = 'PrevSibling';
  28. /**
  29. * Next sibling position
  30. */
  31. const NEXT_SIBLING = 'NextSibling';
  32. /**
  33. * Last child position
  34. */
  35. const LAST_CHILD = 'LastChild';
  36. /**
  37. * First child position
  38. */
  39. const FIRST_CHILD = 'FirstChild';
  40. /**
  41. * TreeListener
  42. *
  43. * @var AbstractTreeListener
  44. */
  45. protected $listener = null;
  46. /**
  47. * The max number of "right" field of the
  48. * tree in case few root nodes will be persisted
  49. * on one flush for node classes
  50. *
  51. * @var array
  52. */
  53. private $treeEdges = array();
  54. /**
  55. * Stores a list of node position strategies
  56. * for each node by object hash
  57. *
  58. * @var array
  59. */
  60. private $nodePositions = array();
  61. /**
  62. * Stores a list of delayed nodes for correct order of updates
  63. *
  64. * @var array
  65. */
  66. private $delayedNodes = array();
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function __construct(TreeListener $listener)
  71. {
  72. $this->listener = $listener;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getName()
  78. {
  79. return Strategy::NESTED;
  80. }
  81. /**
  82. * Set node position strategy
  83. *
  84. * @param string $oid
  85. * @param string $position
  86. */
  87. public function setNodePosition($oid, $position)
  88. {
  89. $valid = array(
  90. self::FIRST_CHILD,
  91. self::LAST_CHILD,
  92. self::NEXT_SIBLING,
  93. self::PREV_SIBLING
  94. );
  95. if (!in_array($position, $valid, false)) {
  96. throw new \Gedmo\Exception\InvalidArgumentException("Position: {$position} is not valid in nested set tree");
  97. }
  98. $this->nodePositions[$oid] = $position;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function processScheduledInsertion($em, $node, AdapterInterface $ea)
  104. {
  105. $meta = $em->getClassMetadata(get_class($node));
  106. $config = $this->listener->getConfiguration($em, $meta->name);
  107. $meta->getReflectionProperty($config['left'])->setValue($node, 0);
  108. $meta->getReflectionProperty($config['right'])->setValue($node, 0);
  109. if (isset($config['level'])) {
  110. $meta->getReflectionProperty($config['level'])->setValue($node, 0);
  111. }
  112. if (isset($config['root'])) {
  113. $meta->getReflectionProperty($config['root'])->setValue($node, 0);
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function processScheduledUpdate($em, $node, AdapterInterface $ea)
  120. {
  121. $meta = $em->getClassMetadata(get_class($node));
  122. $config = $this->listener->getConfiguration($em, $meta->name);
  123. $uow = $em->getUnitOfWork();
  124. $changeSet = $uow->getEntityChangeSet($node);
  125. if (isset($config['root']) && isset($changeSet[$config['root']])) {
  126. throw new \Gedmo\Exception\UnexpectedValueException("Root cannot be changed manualy, change parent instead");
  127. }
  128. $oid = spl_object_hash($node);
  129. if (isset($changeSet[$config['left']]) && isset($this->nodePositions[$oid])) {
  130. $wrapped = AbstractWrapper::wrap($node, $em);
  131. $parent = $wrapped->getPropertyValue($config['parent']);
  132. // revert simulated changeset
  133. $uow->clearEntityChangeSet($oid);
  134. $wrapped->setPropertyValue($config['left'], $changeSet[$config['left']][0]);
  135. $uow->setOriginalEntityProperty($oid, $config['left'], $changeSet[$config['left']][0]);
  136. // set back all other changes
  137. foreach ($changeSet as $field => $set) {
  138. if ($field !== $config['left']) {
  139. $uow->setOriginalEntityProperty($oid, $field, $set[0]);
  140. $wrapped->setPropertyValue($field, $set[1]);
  141. }
  142. }
  143. $uow->recomputeSingleEntityChangeSet($meta, $node);
  144. $this->updateNode($em, $node, $parent);
  145. } elseif (isset($changeSet[$config['parent']])) {
  146. $this->updateNode($em, $node, $changeSet[$config['parent']][1]);
  147. }
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function processPostPersist($em, $node, AdapterInterface $ea)
  153. {
  154. $meta = $em->getClassMetadata(get_class($node));
  155. $config = $this->listener->getConfiguration($em, $meta->name);
  156. $parent = $meta->getReflectionProperty($config['parent'])->getValue($node);
  157. $this->updateNode($em, $node, $parent, self::LAST_CHILD);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function processScheduledDelete($em, $node)
  163. {
  164. $meta = $em->getClassMetadata(get_class($node));
  165. $config = $this->listener->getConfiguration($em, $meta->name);
  166. $uow = $em->getUnitOfWork();
  167. $wrapped = AbstractWrapper::wrap($node, $em);
  168. $leftValue = $wrapped->getPropertyValue($config['left']);
  169. $rightValue = $wrapped->getPropertyValue($config['right']);
  170. if (!$leftValue || !$rightValue) {
  171. return;
  172. }
  173. $rootId = isset($config['root']) ? $wrapped->getPropertyValue($config['root']) : null;
  174. $diff = $rightValue - $leftValue + 1;
  175. if ($diff > 2) {
  176. $qb = $em->createQueryBuilder();
  177. $qb->select('node')
  178. ->from($config['useObjectClass'], 'node')
  179. ->where($qb->expr()->between('node.'.$config['left'], '?1', '?2'))
  180. ->setParameters(array(1 => $leftValue, 2 => $rightValue))
  181. ;
  182. if (isset($config['root'])) {
  183. $qb->andWhere($rootId === null ?
  184. $qb->expr()->isNull('node.'.$config['root']) :
  185. $qb->expr()->eq('node.'.$config['root'], is_string($rootId) ? $qb->expr()->literal($rootId) : $rootId)
  186. );
  187. }
  188. $q = $qb->getQuery();
  189. // get nodes for deletion
  190. $nodes = $q->getResult();
  191. foreach ((array)$nodes as $removalNode) {
  192. $uow->scheduleForDelete($removalNode);
  193. }
  194. }
  195. $this->shiftRL($em, $config['useObjectClass'], $rightValue + 1, -$diff, $rootId);
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function onFlushEnd($em, AdapterInterface $ea)
  201. {
  202. // reset values
  203. $this->treeEdges = array();
  204. $this->updatesOnNodeClasses = array();
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function processPreRemove($em, $node)
  210. {}
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function processPrePersist($em, $node)
  215. {}
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function processPreUpdate($em, $node)
  220. {}
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function processMetadataLoad($em, $meta)
  225. {}
  226. /**
  227. * {@inheritdoc}
  228. */
  229. public function processPostUpdate($em, $entity, AdapterInterface $ea)
  230. {}
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function processPostRemove($em, $entity, AdapterInterface $ea)
  235. {}
  236. /**
  237. * Update the $node with a diferent $parent
  238. * destination
  239. *
  240. * @param EntityManager $em
  241. * @param object $node - target node
  242. * @param object $parent - destination node
  243. * @param string $position
  244. * @throws Gedmo\Exception\UnexpectedValueException
  245. * @return void
  246. */
  247. public function updateNode(EntityManager $em, $node, $parent, $position = 'FirstChild')
  248. {
  249. $wrapped = AbstractWrapper::wrap($node, $em);
  250. $meta = $wrapped->getMetadata();
  251. $config = $this->listener->getConfiguration($em, $meta->name);
  252. $rootId = isset($config['root']) ? $wrapped->getPropertyValue($config['root']) : null;
  253. $identifierField = $meta->getSingleIdentifierFieldName();
  254. $nodeId = $wrapped->getIdentifier();
  255. $left = $wrapped->getPropertyValue($config['left']);
  256. $right = $wrapped->getPropertyValue($config['right']);
  257. $isNewNode = empty($left) && empty($right);
  258. if ($isNewNode) {
  259. $left = 1;
  260. $right = 2;
  261. }
  262. $oid = spl_object_hash($node);
  263. if (isset($this->nodePositions[$oid])) {
  264. $position = $this->nodePositions[$oid];
  265. }
  266. $level = 0;
  267. $treeSize = $right - $left + 1;
  268. $newRootId = null;
  269. if ($parent) {
  270. $wrappedParent = AbstractWrapper::wrap($parent, $em);
  271. $parentRootId = isset($config['root']) ? $wrappedParent->getPropertyValue($config['root']) : null;
  272. $parentOid = spl_object_hash($parent);
  273. $parentLeft = $wrappedParent->getPropertyValue($config['left']);
  274. $parentRight = $wrappedParent->getPropertyValue($config['right']);
  275. if (empty($parentLeft) && empty($parentRight)) {
  276. // parent node is a new node, but wasn't processed yet (due to Doctrine commit order calculator redordering)
  277. // We delay processing of node to the moment parent node will be processed
  278. if (!isset($this->delayedNodes[$parentOid])) {
  279. $this->delayedNodes[$parentOid] = array();
  280. }
  281. $this->delayedNodes[$parentOid][] = array('node' => $node, 'position' => $position);
  282. return;
  283. }
  284. if (!$isNewNode && $rootId === $parentRootId && $parentLeft >= $left && $parentRight <= $right) {
  285. throw new UnexpectedValueException("Cannot set child as parent to node: {$nodeId}");
  286. }
  287. if (isset($config['level'])) {
  288. $level = $wrappedParent->getPropertyValue($config['level']);
  289. }
  290. switch ($position) {
  291. case self::PREV_SIBLING:
  292. if (property_exists($node, 'sibling')) {
  293. $wrappedSibling = AbstractWrapper::wrap($node->sibling, $em);
  294. $start = $wrappedSibling->getPropertyValue($config['left']);
  295. $level++;
  296. } else {
  297. $newParent = $wrappedParent->getPropertyValue($config['parent']);
  298. if (is_null($newParent) && (isset($config['root']) || $isNewNode)) {
  299. throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible");
  300. }
  301. $wrapped->setPropertyValue($config['parent'], $newParent);
  302. $em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $node);
  303. $start = $parentLeft;
  304. }
  305. break;
  306. case self::NEXT_SIBLING:
  307. if (property_exists($node, 'sibling')) {
  308. $wrappedSibling = AbstractWrapper::wrap($node->sibling, $em);
  309. $start = $wrappedSibling->getPropertyValue($config['right']) + 1;
  310. $level++;
  311. } else {
  312. $newParent = $wrappedParent->getPropertyValue($config['parent']);
  313. if (is_null($newParent) && (isset($config['root']) || $isNewNode)) {
  314. throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible");
  315. }
  316. $wrapped->setPropertyValue($config['parent'], $newParent);
  317. $em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $node);
  318. $start = $parentRight + 1;
  319. }
  320. break;
  321. case self::LAST_CHILD:
  322. $start = $parentRight;
  323. $level++;
  324. break;
  325. case self::FIRST_CHILD:
  326. default:
  327. $start = $parentLeft + 1;
  328. $level++;
  329. break;
  330. }
  331. $this->shiftRL($em, $config['useObjectClass'], $start, $treeSize, $parentRootId);
  332. if (!$isNewNode && $rootId === $parentRootId && $left >= $start) {
  333. $left += $treeSize;
  334. $wrapped->setPropertyValue($config['left'], $left);
  335. }
  336. if (!$isNewNode && $rootId === $parentRootId && $right >= $start) {
  337. $right += $treeSize;
  338. $wrapped->setPropertyValue($config['right'], $right);
  339. }
  340. $newRootId = $parentRootId;
  341. } elseif (!isset($config['root'])) {
  342. $start = isset($this->treeEdges[$meta->name]) ?
  343. $this->treeEdges[$meta->name] : $this->max($em, $config['useObjectClass']);
  344. $this->treeEdges[$meta->name] = $start + 2;
  345. $start++;
  346. } else {
  347. $start = 1;
  348. $newRootId = $nodeId;
  349. }
  350. $diff = $start - $left;
  351. if (!$isNewNode) {
  352. $levelDiff = isset($config['level']) ? $level - $wrapped->getPropertyValue($config['level']) : null;
  353. $this->shiftRangeRL(
  354. $em,
  355. $config['useObjectClass'],
  356. $left,
  357. $right,
  358. $diff,
  359. $rootId,
  360. $newRootId,
  361. $levelDiff
  362. );
  363. $this->shiftRL($em, $config['useObjectClass'], $left, -$treeSize, $rootId);
  364. } else {
  365. $qb = $em->createQueryBuilder();
  366. $qb->update($config['useObjectClass'], 'node');
  367. if (isset($config['root'])) {
  368. $qb->set('node.'.$config['root'], null === $newRootId ?
  369. 'NULL' :
  370. (is_string($newRootId) ? $qb->expr()->literal($newRootId) : $newRootId)
  371. );
  372. $wrapped->setPropertyValue($config['root'], $newRootId);
  373. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['root'], $newRootId);
  374. }
  375. if (isset($config['level'])) {
  376. $qb->set('node.' . $config['level'], $level);
  377. $wrapped->setPropertyValue($config['level'], $level);
  378. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['level'], $level);
  379. }
  380. if (isset($newParent)) {
  381. $wrappedNewParent = AbstractWrapper::wrap($newParent, $em);
  382. $newParentId = $wrappedNewParent->getIdentifier();
  383. $qb->set('node.'.$config['parent'], null === $newParentId ?
  384. 'NULL' :
  385. (is_string($newParentId) ? $qb->expr()->literal($newParentId) : $newParentId)
  386. );
  387. $wrapped->setPropertyValue($config['parent'], $newParent);
  388. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['parent'], $newParent);
  389. }
  390. $qb->set('node.' . $config['left'], $left + $diff);
  391. $qb->set('node.' . $config['right'], $right + $diff);
  392. // node id cannot be null
  393. $qb->where($qb->expr()->eq('node.'.$identifierField, is_string($nodeId) ? $qb->expr()->literal($nodeId) : $nodeId));
  394. $qb->getQuery()->getSingleScalarResult();
  395. $wrapped->setPropertyValue($config['left'], $left + $diff);
  396. $wrapped->setPropertyValue($config['right'], $right + $diff);
  397. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['left'], $left + $diff);
  398. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['right'], $right + $diff);
  399. }
  400. if (isset($this->delayedNodes[$oid])) {
  401. foreach($this->delayedNodes[$oid] as $nodeData) {
  402. $this->updateNode($em, $nodeData['node'], $node, $nodeData['position']);
  403. }
  404. }
  405. }
  406. /**
  407. * Get the edge of tree
  408. *
  409. * @param EntityManager $em
  410. * @param string $class
  411. * @param integer $rootId
  412. * @return integer
  413. */
  414. public function max(EntityManager $em, $class, $rootId = 0)
  415. {
  416. $meta = $em->getClassMetadata($class);
  417. $config = $this->listener->getConfiguration($em, $meta->name);
  418. $qb = $em->createQueryBuilder();
  419. $qb->select($qb->expr()->max('node.'.$config['right']))
  420. ->from($config['useObjectClass'], 'node')
  421. ;
  422. if (isset($config['root']) && $rootId) {
  423. $qb->where($rootId === null ?
  424. $qb->expr()->isNull('node.'.$config['root']) :
  425. $qb->expr()->eq('node.'.$config['root'], is_string($rootId) ? $qb->expr()->literal($rootId) : $rootId)
  426. );
  427. }
  428. $query = $qb->getQuery();
  429. $right = $query->getSingleScalarResult();
  430. return intval($right);
  431. }
  432. /**
  433. * Shift tree left and right values by delta
  434. *
  435. * @param EntityManager $em
  436. * @param string $class
  437. * @param integer $first
  438. * @param integer $delta
  439. * @param integer|string $rootId
  440. * @return void
  441. */
  442. public function shiftRL(EntityManager $em, $class, $first, $delta, $rootId = null)
  443. {
  444. $meta = $em->getClassMetadata($class);
  445. $config = $this->listener->getConfiguration($em, $class);
  446. $sign = ($delta >= 0) ? ' + ' : ' - ';
  447. $absDelta = abs($delta);
  448. $qb = $em->createQueryBuilder();
  449. $qb->update($config['useObjectClass'], 'node')
  450. ->set('node.'.$config['left'], "node.{$config['left']} {$sign} {$absDelta}")
  451. ->where($qb->expr()->gte('node.'.$config['left'], $first))
  452. ;
  453. if (isset($config['root'])) {
  454. $qb->andWhere($rootId === null ?
  455. $qb->expr()->isNull('node.'.$config['root']) :
  456. $qb->expr()->eq('node.'.$config['root'], is_string($rootId) ? $qb->expr()->literal($rootId) : $rootId)
  457. );
  458. }
  459. $qb->getQuery()->getSingleScalarResult();
  460. $qb = $em->createQueryBuilder();
  461. $qb->update($config['useObjectClass'], 'node')
  462. ->set('node.'.$config['right'], "node.{$config['right']} {$sign} {$absDelta}")
  463. ->where($qb->expr()->gte('node.'.$config['right'], $first))
  464. ;
  465. if (isset($config['root'])) {
  466. $qb->andWhere($rootId === null ?
  467. $qb->expr()->isNull('node.'.$config['root']) :
  468. $qb->expr()->eq('node.'.$config['root'], is_string($rootId) ? $qb->expr()->literal($rootId) : $rootId)
  469. );
  470. }
  471. $qb->getQuery()->getSingleScalarResult();
  472. // update in memory nodes increases performance, saves some IO
  473. foreach ($em->getUnitOfWork()->getIdentityMap() as $className => $nodes) {
  474. // for inheritance mapped classes, only root is always in the identity map
  475. if ($className !== $meta->rootEntityName) {
  476. continue;
  477. }
  478. foreach ($nodes as $node) {
  479. if ($node instanceof Proxy && !$node->__isInitialized__) {
  480. continue;
  481. }
  482. $oid = spl_object_hash($node);
  483. $left = $meta->getReflectionProperty($config['left'])->getValue($node);
  484. $root = isset($config['root']) ? $meta->getReflectionProperty($config['root'])->getValue($node) : null;
  485. if ($root === $rootId && $left >= $first) {
  486. $meta->getReflectionProperty($config['left'])->setValue($node, $left + $delta);
  487. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['left'], $left + $delta);
  488. }
  489. $right = $meta->getReflectionProperty($config['right'])->getValue($node);
  490. if ($root === $rootId && $right >= $first) {
  491. $meta->getReflectionProperty($config['right'])->setValue($node, $right + $delta);
  492. $em->getUnitOfWork()->setOriginalEntityProperty($oid, $config['right'], $right + $delta);
  493. }
  494. }
  495. }
  496. }
  497. /**
  498. * Shift range of right and left values on tree
  499. * depending on tree level diference also
  500. *
  501. * @param EntityManager $em
  502. * @param string $class
  503. * @param integer $first
  504. * @param integer $last
  505. * @param integer $delta
  506. * @param integer|string $rootId
  507. * @param integer|string $destRootId
  508. * @param integer $levelDelta
  509. * @return void
  510. */
  511. public function shiftRangeRL(EntityManager $em, $class, $first, $last, $delta, $rootId = null, $destRootId = null, $levelDelta = null)
  512. {
  513. $meta = $em->getClassMetadata($class);
  514. $config = $this->listener->getConfiguration($em, $class);
  515. $sign = ($delta >= 0) ? ' + ' : ' - ';
  516. $absDelta = abs($delta);
  517. $levelSign = ($levelDelta >= 0) ? ' + ' : ' - ';
  518. $absLevelDelta = abs($levelDelta);
  519. $qb = $em->createQueryBuilder();
  520. $qb->update($config['useObjectClass'], 'node')
  521. ->set('node.'.$config['left'], "node.{$config['left']} {$sign} {$absDelta}")
  522. ->set('node.'.$config['right'], "node.{$config['right']} {$sign} {$absDelta}")
  523. ->where($qb->expr()->gte('node.'.$config['left'], $first))
  524. ->andWhere($qb->expr()->lte('node.'.$config['right'], $last))
  525. ;
  526. if (isset($config['root'])) {
  527. $qb->set(
  528. 'node.'.$config['root'],
  529. is_string($destRootId) ? $qb->expr()->literal($destRootId) : $destRootId
  530. );
  531. $qb->andWhere($rootId === null ?
  532. $qb->expr()->isNull('node.'.$config['root']) :
  533. $qb->expr()->eq('node.'.$config['root'], is_string($rootId) ? $qb->expr()->literal($rootId) : $rootId)
  534. );
  535. }
  536. if (isset($config['level'])) {
  537. $qb->set('node.'.$config['level'], "node.{$config['level']} {$levelSign} {$absLevelDelta}");
  538. }
  539. $qb->getQuery()->getSingleScalarResult();
  540. // update in memory nodes increases performance, saves some IO
  541. foreach ($em->getUnitOfWork()->getIdentityMap() as $className => $nodes) {
  542. // for inheritance mapped classes, only root is always in the identity map
  543. if ($className !== $meta->rootEntityName) {
  544. continue;
  545. }
  546. foreach ($nodes as $node) {
  547. if ($node instanceof Proxy && !$node->__isInitialized__) {
  548. continue;
  549. }
  550. $left = $meta->getReflectionProperty($config['left'])->getValue($node);
  551. $right = $meta->getReflectionProperty($config['right'])->getValue($node);
  552. $root = isset($config['root']) ? $meta->getReflectionProperty($config['root'])->getValue($node) : null;
  553. if ($root === $rootId && $left >= $first && $right <= $last) {
  554. $oid = spl_object_hash($node);
  555. $uow = $em->getUnitOfWork();
  556. $meta->getReflectionProperty($config['left'])->setValue($node, $left + $delta);
  557. $uow->setOriginalEntityProperty($oid, $config['left'], $left + $delta);
  558. $meta->getReflectionProperty($config['right'])->setValue($node, $right + $delta);
  559. $uow->setOriginalEntityProperty($oid, $config['right'], $right + $delta);
  560. if (isset($config['root'])) {
  561. $meta->getReflectionProperty($config['root'])->setValue($node, $destRootId);
  562. $uow->setOriginalEntityProperty($oid, $config['root'], $destRootId);
  563. }
  564. if (isset($config['level'])) {
  565. $level = $meta->getReflectionProperty($config['level'])->getValue($node);
  566. $meta->getReflectionProperty($config['level'])->setValue($node, $level + $levelDelta);
  567. $uow->setOriginalEntityProperty($oid, $config['level'], $level + $levelDelta);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }