Nested.php 24 KB

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