@@ -286,10 +286,12 @@ public function processPostRemove($em, $entity, AdapterInterface $ea)
286
286
public function updateNode (EntityManager $ em , $ node , $ parent , $ position = 'FirstChild ' )
287
287
{
288
288
$ wrapped = AbstractWrapper::wrap ($ node , $ em );
289
+
290
+ /** @var ClassMetadata $meta */
289
291
$ meta = $ wrapped ->getMetadata ();
290
292
$ config = $ this ->listener ->getConfiguration ($ em , $ meta ->name );
291
293
292
- $ rootId = isset ($ config ['root ' ]) ? $ wrapped ->getPropertyValue ($ config ['root ' ]) : null ;
294
+ $ root = isset ($ config ['root ' ]) ? $ wrapped ->getPropertyValue ($ config ['root ' ]) : null ;
293
295
$ identifierField = $ meta ->getSingleIdentifierFieldName ();
294
296
$ nodeId = $ wrapped ->getIdentifier ();
295
297
@@ -308,11 +310,11 @@ public function updateNode(EntityManager $em, $node, $parent, $position = 'First
308
310
}
309
311
$ level = 0 ;
310
312
$ treeSize = $ right - $ left + 1 ;
311
- $ newRootId = null ;
313
+ $ newRoot = null ;
312
314
if ($ parent ) {
313
315
$ wrappedParent = AbstractWrapper::wrap ($ parent , $ em );
314
316
315
- $ parentRootId = isset ($ config ['root ' ]) ? $ wrappedParent ->getPropertyValue ($ config ['root ' ]) : null ;
317
+ $ parentRoot = isset ($ config ['root ' ]) ? $ wrappedParent ->getPropertyValue ($ config ['root ' ]) : null ;
316
318
$ parentOid = spl_object_hash ($ parent );
317
319
$ parentLeft = $ wrappedParent ->getPropertyValue ($ config ['left ' ]);
318
320
$ parentRight = $ wrappedParent ->getPropertyValue ($ config ['right ' ]);
@@ -326,7 +328,7 @@ public function updateNode(EntityManager $em, $node, $parent, $position = 'First
326
328
327
329
return ;
328
330
}
329
- if (!$ isNewNode && $ rootId === $ parentRootId && $ parentLeft >= $ left && $ parentRight <= $ right ) {
331
+ if (!$ isNewNode && $ root === $ parentRoot && $ parentLeft >= $ left && $ parentRight <= $ right ) {
330
332
throw new UnexpectedValueException ("Cannot set child as parent to node: {$ nodeId }" );
331
333
}
332
334
if (isset ($ config ['level ' ])) {
@@ -376,24 +378,29 @@ public function updateNode(EntityManager $em, $node, $parent, $position = 'First
376
378
$ level ++;
377
379
break ;
378
380
}
379
- $ this ->shiftRL ($ em , $ config ['useObjectClass ' ], $ start , $ treeSize , $ parentRootId );
380
- if (!$ isNewNode && $ rootId === $ parentRootId && $ left >= $ start ) {
381
+ $ this ->shiftRL ($ em , $ config ['useObjectClass ' ], $ start , $ treeSize , $ parentRoot );
382
+ if (!$ isNewNode && $ root === $ parentRoot && $ left >= $ start ) {
381
383
$ left += $ treeSize ;
382
384
$ wrapped ->setPropertyValue ($ config ['left ' ], $ left );
383
385
}
384
- if (!$ isNewNode && $ rootId === $ parentRootId && $ right >= $ start ) {
386
+ if (!$ isNewNode && $ root === $ parentRoot && $ right >= $ start ) {
385
387
$ right += $ treeSize ;
386
388
$ wrapped ->setPropertyValue ($ config ['right ' ], $ right );
387
389
}
388
- $ newRootId = $ parentRootId ;
390
+ $ newRoot = $ parentRoot ;
389
391
} elseif (!isset ($ config ['root ' ])) {
390
392
$ start = isset ($ this ->treeEdges [$ meta ->name ]) ?
391
393
$ this ->treeEdges [$ meta ->name ] : $ this ->max ($ em , $ config ['useObjectClass ' ]);
392
394
$ this ->treeEdges [$ meta ->name ] = $ start + 2 ;
393
395
$ start ++;
394
396
} else {
395
397
$ start = 1 ;
396
- $ newRootId = $ nodeId ;
398
+
399
+ if ($ meta ->isSingleValuedAssociation ($ config ['root ' ])) {
400
+ $ newRoot = $ node ;
401
+ } else {
402
+ $ newRoot = $ wrapped ->getIdentifier ();
403
+ }
397
404
}
398
405
399
406
$ diff = $ start - $ left ;
@@ -405,19 +412,19 @@ public function updateNode(EntityManager $em, $node, $parent, $position = 'First
405
412
$ left ,
406
413
$ right ,
407
414
$ diff ,
408
- $ rootId ,
409
- $ newRootId ,
415
+ $ root ,
416
+ $ newRoot ,
410
417
$ levelDiff
411
418
);
412
- $ this ->shiftRL ($ em , $ config ['useObjectClass ' ], $ left , -$ treeSize , $ rootId );
419
+ $ this ->shiftRL ($ em , $ config ['useObjectClass ' ], $ left , -$ treeSize , $ root );
413
420
} else {
414
421
$ qb = $ em ->createQueryBuilder ();
415
422
$ qb ->update ($ config ['useObjectClass ' ], 'node ' );
416
423
if (isset ($ config ['root ' ])) {
417
424
$ qb ->set ('node. ' .$ config ['root ' ], ':rid ' );
418
- $ qb ->setParameter ('rid ' , $ newRootId );
419
- $ wrapped ->setPropertyValue ($ config ['root ' ], $ newRootId );
420
- $ em ->getUnitOfWork ()->setOriginalEntityProperty ($ oid , $ config ['root ' ], $ newRootId );
425
+ $ qb ->setParameter ('rid ' , $ newRoot );
426
+ $ wrapped ->setPropertyValue ($ config ['root ' ], $ newRoot );
427
+ $ em ->getUnitOfWork ()->setOriginalEntityProperty ($ oid , $ config ['root ' ], $ newRoot );
421
428
}
422
429
if (isset ($ config ['level ' ])) {
423
430
$ qb ->set ('node. ' .$ config ['level ' ], $ level );
@@ -485,9 +492,9 @@ public function max(EntityManager $em, $class, $rootId = 0)
485
492
* @param string $class
486
493
* @param integer $first
487
494
* @param integer $delta
488
- * @param integer|string $rootId
495
+ * @param integer|string $root
489
496
*/
490
- public function shiftRL (EntityManager $ em , $ class , $ first , $ delta , $ rootId = null )
497
+ public function shiftRL (EntityManager $ em , $ class , $ first , $ delta , $ root = null )
491
498
{
492
499
$ meta = $ em ->getClassMetadata ($ class );
493
500
$ config = $ this ->listener ->getConfiguration ($ em , $ class );
@@ -501,7 +508,7 @@ public function shiftRL(EntityManager $em, $class, $first, $delta, $rootId = nul
501
508
;
502
509
if (isset ($ config ['root ' ])) {
503
510
$ qb ->andWhere ($ qb ->expr ()->eq ('node. ' .$ config ['root ' ], ':rid ' ));
504
- $ qb ->setParameter ('rid ' , $ rootId );
511
+ $ qb ->setParameter ('rid ' , $ root );
505
512
}
506
513
$ qb ->getQuery ()->getSingleScalarResult ();
507
514
@@ -512,7 +519,7 @@ public function shiftRL(EntityManager $em, $class, $first, $delta, $rootId = nul
512
519
;
513
520
if (isset ($ config ['root ' ])) {
514
521
$ qb ->andWhere ($ qb ->expr ()->eq ('node. ' .$ config ['root ' ], ':rid ' ));
515
- $ qb ->setParameter ('rid ' , $ rootId );
522
+ $ qb ->setParameter ('rid ' , $ root );
516
523
}
517
524
518
525
$ qb ->getQuery ()->getSingleScalarResult ();
@@ -528,13 +535,13 @@ public function shiftRL(EntityManager $em, $class, $first, $delta, $rootId = nul
528
535
}
529
536
$ oid = spl_object_hash ($ node );
530
537
$ left = $ meta ->getReflectionProperty ($ config ['left ' ])->getValue ($ node );
531
- $ root = isset ($ config ['root ' ]) ? $ meta ->getReflectionProperty ($ config ['root ' ])->getValue ($ node ) : null ;
532
- if ($ root === $ rootId && $ left >= $ first ) {
538
+ $ currentRoot = isset ($ config ['root ' ]) ? $ meta ->getReflectionProperty ($ config ['root ' ])->getValue ($ node ) : null ;
539
+ if ($ currentRoot === $ root && $ left >= $ first ) {
533
540
$ meta ->getReflectionProperty ($ config ['left ' ])->setValue ($ node , $ left + $ delta );
534
541
$ em ->getUnitOfWork ()->setOriginalEntityProperty ($ oid , $ config ['left ' ], $ left + $ delta );
535
542
}
536
543
$ right = $ meta ->getReflectionProperty ($ config ['right ' ])->getValue ($ node );
537
- if ($ root === $ rootId && $ right >= $ first ) {
544
+ if ($ currentRoot === $ root && $ right >= $ first ) {
538
545
$ meta ->getReflectionProperty ($ config ['right ' ])->setValue ($ node , $ right + $ delta );
539
546
$ em ->getUnitOfWork ()->setOriginalEntityProperty ($ oid , $ config ['right ' ], $ right + $ delta );
540
547
}
@@ -551,11 +558,11 @@ public function shiftRL(EntityManager $em, $class, $first, $delta, $rootId = nul
551
558
* @param integer $first
552
559
* @param integer $last
553
560
* @param integer $delta
554
- * @param integer|string $rootId
555
- * @param integer|string $destRootId
561
+ * @param integer|string $root
562
+ * @param integer|string $destRoot
556
563
* @param integer $levelDelta
557
564
*/
558
- public function shiftRangeRL (EntityManager $ em , $ class , $ first , $ last , $ delta , $ rootId = null , $ destRootId = null , $ levelDelta = null )
565
+ public function shiftRangeRL (EntityManager $ em , $ class , $ first , $ last , $ delta , $ root = null , $ destRoot = null , $ levelDelta = null )
559
566
{
560
567
$ meta = $ em ->getClassMetadata ($ class );
561
568
$ config = $ this ->listener ->getConfiguration ($ em , $ class );
@@ -574,9 +581,9 @@ public function shiftRangeRL(EntityManager $em, $class, $first, $last, $delta, $
574
581
;
575
582
if (isset ($ config ['root ' ])) {
576
583
$ qb ->set ('node. ' .$ config ['root ' ], ':drid ' );
577
- $ qb ->setParameter ('drid ' , $ destRootId );
584
+ $ qb ->setParameter ('drid ' , $ destRoot );
578
585
$ qb ->andWhere ($ qb ->expr ()->eq ('node. ' .$ config ['root ' ], ':rid ' ));
579
- $ qb ->setParameter ('rid ' , $ rootId );
586
+ $ qb ->setParameter ('rid ' , $ root );
580
587
}
581
588
if (isset ($ config ['level ' ])) {
582
589
$ qb ->set ('node. ' .$ config ['level ' ], "node. {$ config ['level ' ]} {$ levelSign } {$ absLevelDelta }" );
@@ -594,8 +601,8 @@ public function shiftRangeRL(EntityManager $em, $class, $first, $last, $delta, $
594
601
}
595
602
$ left = $ meta ->getReflectionProperty ($ config ['left ' ])->getValue ($ node );
596
603
$ right = $ meta ->getReflectionProperty ($ config ['right ' ])->getValue ($ node );
597
- $ root = isset ($ config ['root ' ]) ? $ meta ->getReflectionProperty ($ config ['root ' ])->getValue ($ node ) : null ;
598
- if ($ root === $ rootId && $ left >= $ first && $ right <= $ last ) {
604
+ $ currentRoot = isset ($ config ['root ' ]) ? $ meta ->getReflectionProperty ($ config ['root ' ])->getValue ($ node ) : null ;
605
+ if ($ currentRoot === $ root && $ left >= $ first && $ right <= $ last ) {
599
606
$ oid = spl_object_hash ($ node );
600
607
$ uow = $ em ->getUnitOfWork ();
601
608
@@ -604,8 +611,8 @@ public function shiftRangeRL(EntityManager $em, $class, $first, $last, $delta, $
604
611
$ meta ->getReflectionProperty ($ config ['right ' ])->setValue ($ node , $ right + $ delta );
605
612
$ uow ->setOriginalEntityProperty ($ oid , $ config ['right ' ], $ right + $ delta );
606
613
if (isset ($ config ['root ' ])) {
607
- $ meta ->getReflectionProperty ($ config ['root ' ])->setValue ($ node , $ destRootId );
608
- $ uow ->setOriginalEntityProperty ($ oid , $ config ['root ' ], $ destRootId );
614
+ $ meta ->getReflectionProperty ($ config ['root ' ])->setValue ($ node , $ destRoot );
615
+ $ uow ->setOriginalEntityProperty ($ oid , $ config ['root ' ], $ destRoot );
609
616
}
610
617
if (isset ($ config ['level ' ])) {
611
618
$ level = $ meta ->getReflectionProperty ($ config ['level ' ])->getValue ($ node );
0 commit comments