@@ -332,6 +332,15 @@ protected function preprocessSave(BeforeSaveEvent $event, bool $new = false): vo
332332 $ data = $ event ->data ;
333333 /** @var NestedEntityInterface $entity */
334334 $ entity = $ this ->toEntity ($ event ->source );
335+
336+ if (is_array ($ event ->source )) {
337+ $ position = $ event ->source ['position ' ];
338+ } else {
339+ $ position = $ event ->source ->getPosition ();
340+ }
341+
342+ $ this ->hydrate (compact ('position ' ), $ entity );
343+
335344 $ position = $ entity ->getPosition ();
336345 $ className = $ this ->getMetadata ()->getClassName ();
337346
@@ -353,11 +362,18 @@ protected function preprocessSave(BeforeSaveEvent $event, bool $new = false): vo
353362
354363 // We are inserting a node relative to the last root node.
355364 if (!$ position ->getReferenceId ()) {
365+ $ parentId = $ entity ->getParentId () ?: $ this ->getEmptyParentId ();
356366 $ reference = $ this ->select ($ k , 'parent_id ' , 'level ' , 'lft ' , 'rgt ' )
357- ->where ('id ' , $ entity -> getParentId () ?: $ this -> getEmptyParentId () )
367+ ->where ('id ' , $ parentId )
358368 ->order ('lft ' , 'DESC ' )
359369 ->limit (1 )
360370 ->get ($ className );
371+
372+ if (!$ reference ) {
373+ throw new NestedHandleException (
374+ sprintf ('Reference of parent %s not found. ' , $ parentId )
375+ );
376+ }
361377 } else {
362378 // We have a real node set as a location reference.
363379 // Get the reference node by primary key.
0 commit comments