protected function _movePosition(EntityInterface $entity, $direction = '+')
    {
        if ($entity->isNew()) {
            return false;
        }

        $scope = $this->_getScope($entity);
        if ($scope === false) {
            return false;
        }

        $config = $this->getConfig();
        $table = $this->_table;

        $table->removeBehavior('Sequence');

        $return = $table->getConnection()->transactional(
            function ($connection) use ($table, $entity, $config, $scope, $direction) {
                $orderField = $config['order'];
                // Nothing to do if trying to move up entity already at first position
                if ($direction === '-' && $entity->get($orderField) === $config['start']) {
                    return true;
                }

                $oldOrder = $entity->get($orderField);
                $newOrder = $entity->get($orderField) - 1;
                if ($direction === '+') {
                    $newOrder = $entity->get($orderField) + 1;
                }

                $previousEntity = $table->find()
                    ->where(array_merge($scope, [$orderField => $newOrder]))
                    ->first();

                if (!empty($previousEntity)) {
                    $previousEntity->set($orderField, $oldOrder);
                    if (!$table->save($previousEntity, ['atomic' => false, 'checkRules' => false])) {
                        return false;
                    }
                // Nothing to do if trying to move down entity already at last position
                } elseif ($direction === '+') {
                    return true;
                }

                $entity->set($orderField, $newOrder);

                return $table->save($entity, ['atomic' => false, 'checkRules' => false]);
            }
        );

        $table->addBehavior('ADmad/Sequence.Sequence', $config);

        return (bool)$return;
    }
protected function _movePosition(EntityInterface $entity, $direction = '+')
    {
        if ($entity->isNew()) {
            return false;
        }

        $scope = $this->_getScope($entity);
        if ($scope === false) {
            return false;
        }

        $config = $this->getConfig();
        $table = $this->_table;

        $table->removeBehavior('Sequence');

        $return = $table->getConnection()->transactional(
            function ($connection) use ($table, $entity, $config, $scope, $direction) {
                $orderField = $config['order'];
                // Nothing to do if trying to move up entity already at first position
                if ($direction === '-' && $entity->get($orderField) === $config['start']) {
                    return true;
                }

                $oldOrder = $entity->get($orderField);
                $newOrder = $entity->get($orderField) - 1;
                if ($direction === '+') {
                    $newOrder = $entity->get($orderField) + 1;
                }

                $previousEntity = $table->find()
                    ->where(array_merge($scope, [$orderField => $newOrder]))
                    ->first();

                if (!empty($previousEntity)) {
                    $previousEntity->set($orderField, $oldOrder);
                    if (!$table->save($previousEntity, ['atomic' => false, 'checkRules' => false])) {
                        return false;
                    }
                // Nothing to do if trying to move down entity already at last position
                } elseif ($direction === '+') {
                    return true;
                }

                $entity->set($orderField, $newOrder);

                return $table->save($entity, ['atomic' => false, 'checkRules' => false]);
            }
        );

        $table->addBehavior('ADmad/Sequence.Sequence', $config);

        return (bool)$return;
    }
protected function _movePosition ( EntityInterface $ entity , $ direction = '+' ) { if ( $ entity -> isNew ( ) ) { return false ; } $ scope = $ this -> _getScope ( $ entity ) ; if ( $ scope === false ) { return false ; } $ config = $ this -> getConfig ( ) ; $ table = $ this -> _table ; $ table -> removeBehavior ( 'Sequence' ) ; $ return = $ table -> getConnection ( ) -> transactional ( function ( $ connection ) use ( $ table , $ entity , $ config , $ scope , $ direction ) { $ orderField = $ config [ 'order' ] ; // Nothing to do if trying to move up entity already at first position if ( $ direction === '-' && $ entity -> get ( $ orderField ) === $ config [ 'start' ] ) { return true ; } $ oldOrder = $ entity -> get ( $ orderField ) ; $ newOrder = $ entity -> get ( $ orderField ) - 1 ; if ( $ direction === '+' ) { $ newOrder = $ entity -> get ( $ orderField ) + 1 ; } $ previousEntity = $ table -> find ( ) -> where ( array_merge ( $ scope , [ $ orderField => $ newOrder ] ) ) -> first ( ) ; if ( ! empty ( $ previousEntity ) ) { $ previousEntity -> set ( $ orderField , $ oldOrder ) ; if ( ! $ table -> save ( $ previousEntity , [ 'atomic' => false , 'checkRules' => false ] ) ) { return false ; } // Nothing to do if trying to move down entity already at last position } elseif ( $ direction === '+' ) { return true ; } $ entity -> set ( $ orderField , $ newOrder ) ; return $ table -> save ( $ entity , [ 'atomic' => false , 'checkRules' => false ] ) ; } ) ; $ table -> addBehavior ( 'ADmad/Sequence.Sequence' , $ config ) ; return ( bool ) $ return ; }
Change the position of the entity on the list by a single position

If an entity that conflicts with the new position already exists, this
will also swap positions with it

@param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved.
@param string $direction Whether to increment or decrement the field.

@return bool
Change the position of the entity on the list by a single position