protected function _setParent($entity, $parent)
    {
        $config = $this->getConfig();
        $parentNode = $this->_getNode($parent);
        $this->_ensureFields($entity);
        $parentLeft = $parentNode->get($config['left']);
        $parentRight = $parentNode->get($config['right']);
        $right = $entity->get($config['right']);
        $left = $entity->get($config['left']);

        if ($parentLeft > $left && $parentLeft < $right) {
            throw new RuntimeException(sprintf(
                'Cannot use node "%s" as parent for entity "%s"',
                $parent,
                $entity->get($this->_getPrimaryKey())
            ));
        }

        // Values for moving to the left
        $diff = $right - $left + 1;
        $targetLeft = $parentRight;
        $targetRight = $diff + $parentRight - 1;
        $min = $parentRight;
        $max = $left - 1;

        if ($left < $targetLeft) {
            // Moving to the right
            $targetLeft = $parentRight - $diff;
            $targetRight = $parentRight - 1;
            $min = $right + 1;
            $max = $parentRight - 1;
            $diff *= -1;
        }

        if ($right - $left > 1) {
            // Correcting internal subtree
            $internalLeft = $left + 1;
            $internalRight = $right - 1;
            $this->_sync($targetLeft - $left, '+', "BETWEEN {$internalLeft} AND {$internalRight}", true);
        }

        $this->_sync($diff, '+', "BETWEEN {$min} AND {$max}");

        if ($right - $left > 1) {
            $this->_unmarkInternalTree();
        }

        // Allocating new position
        $entity->set($config['left'], $targetLeft);
        $entity->set($config['right'], $targetRight);
    }
protected function _setParent($entity, $parent)
    {
        $config = $this->getConfig();
        $parentNode = $this->_getNode($parent);
        $this->_ensureFields($entity);
        $parentLeft = $parentNode->get($config['left']);
        $parentRight = $parentNode->get($config['right']);
        $right = $entity->get($config['right']);
        $left = $entity->get($config['left']);

        if ($parentLeft > $left && $parentLeft < $right) {
            throw new RuntimeException(sprintf(
                'Cannot use node "%s" as parent for entity "%s"',
                $parent,
                $entity->get($this->_getPrimaryKey())
            ));
        }

        // Values for moving to the left
        $diff = $right - $left + 1;
        $targetLeft = $parentRight;
        $targetRight = $diff + $parentRight - 1;
        $min = $parentRight;
        $max = $left - 1;

        if ($left < $targetLeft) {
            // Moving to the right
            $targetLeft = $parentRight - $diff;
            $targetRight = $parentRight - 1;
            $min = $right + 1;
            $max = $parentRight - 1;
            $diff *= -1;
        }

        if ($right - $left > 1) {
            // Correcting internal subtree
            $internalLeft = $left + 1;
            $internalRight = $right - 1;
            $this->_sync($targetLeft - $left, '+', "BETWEEN {$internalLeft} AND {$internalRight}", true);
        }

        $this->_sync($diff, '+', "BETWEEN {$min} AND {$max}");

        if ($right - $left > 1) {
            $this->_unmarkInternalTree();
        }

        // Allocating new position
        $entity->set($config['left'], $targetLeft);
        $entity->set($config['right'], $targetRight);
    }
protected function _setParent ( $ entity , $ parent ) { $ config = $ this -> getConfig ( ) ; $ parentNode = $ this -> _getNode ( $ parent ) ; $ this -> _ensureFields ( $ entity ) ; $ parentLeft = $ parentNode -> get ( $ config [ 'left' ] ) ; $ parentRight = $ parentNode -> get ( $ config [ 'right' ] ) ; $ right = $ entity -> get ( $ config [ 'right' ] ) ; $ left = $ entity -> get ( $ config [ 'left' ] ) ; if ( $ parentLeft > $ left && $ parentLeft < $ right ) { throw new RuntimeException ( sprintf ( 'Cannot use node "%s" as parent for entity "%s"' , $ parent , $ entity -> get ( $ this -> _getPrimaryKey ( ) ) ) ) ; } // Values for moving to the left $ diff = $ right - $ left + 1 ; $ targetLeft = $ parentRight ; $ targetRight = $ diff + $ parentRight - 1 ; $ min = $ parentRight ; $ max = $ left - 1 ; if ( $ left < $ targetLeft ) { // Moving to the right $ targetLeft = $ parentRight - $ diff ; $ targetRight = $ parentRight - 1 ; $ min = $ right + 1 ; $ max = $ parentRight - 1 ; $ diff *= - 1 ; } if ( $ right - $ left > 1 ) { // Correcting internal subtree $ internalLeft = $ left + 1 ; $ internalRight = $ right - 1 ; $ this -> _sync ( $ targetLeft - $ left , '+' , "BETWEEN {$internalLeft} AND {$internalRight}" , true ) ; } $ this -> _sync ( $ diff , '+' , "BETWEEN {$min} AND {$max}" ) ; if ( $ right - $ left > 1 ) { $ this -> _unmarkInternalTree ( ) ; } // Allocating new position $ entity -> set ( $ config [ 'left' ] , $ targetLeft ) ; $ entity -> set ( $ config [ 'right' ] , $ targetRight ) ; }
Sets the correct left and right values for the passed entity so it can be
updated to a new parent. It also makes the hole in the tree so the node
move can be done without corrupting the structure.

@param \Cake\Datasource\EntityInterface $entity The entity to re-parent
@param mixed $parent the id of the parent to set
@return void
@throws \RuntimeException if the parent to set to the entity is not valid
Sets the correct left and right values for the passed entity so it can be updated to a new parent . It also makes the hole in the tree so the node move can be done without corrupting the structure .