public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
    {
        $config = $this->getConfig();

        $newOrder = null;
        $newScope = $this->_getScope($entity);
        if ($newScope === false) {
            return;
        }

        $orderField = $config['order'];
        $newOrder = $entity->get($orderField);

        // Adding
        if ($entity->isNew()) {
            // Order not specified
            if ($newOrder === null) {
                // Insert at end of list
                $entity->set($orderField, $this->_getHighestOrder($newScope) + 1);
                // Order specified
            } else {
                // Increment order of records it's inserted before
                $this->_sync(
                    [$orderField => $this->_getUpdateExpression('+')],
                    [$orderField . ' >=' => $newOrder],
                    $newScope
                );
            }

            // Editing
        } else {
            // No action if no new order or scope specified
            if ($newOrder === null && !$newScope) {
                return;
            }

            list($oldOrder, $oldScope) = $this->_getOldValues($entity);

            // No action if new and old scope and order same
            if ($newOrder == $oldOrder &&
                $newScope == $oldScope
            ) {
                return;
            }

            // If changing scope
            if ($newScope && $newScope != $oldScope) {
                // Decrement records in old scope with higher order than moved record old order
                $this->_sync(
                    [$orderField => $this->_getUpdateExpression('-')],
                    [$orderField . ' >' => $oldOrder],
                    $oldScope
                );

                // Order not specified
                if ($newOrder === null) {
                    // Insert at end of new scope
                    $entity->set(
                        $orderField,
                        $this->_getHighestOrder($newScope) + 1
                    );

                    // Order specified
                } else {
                    // Increment records in new scope with higher order than moved record new order
                    $this->_sync(
                        [$orderField => $this->_getUpdateExpression('+')],
                        [$orderField . ' >=' => $newOrder],
                        $newScope
                    );
                }
                // Same scope
            } else {
                // If moving up
                if ($newOrder < $oldOrder) {
                    // Increment order of those in between
                    $this->_sync(
                        [$orderField => $this->_getUpdateExpression('+')],
                        [
                            $orderField . ' >=' => $newOrder,
                            $orderField . ' <' => $oldOrder,
                        ],
                        $newScope
                    );

                // Moving down
                } else {
                    // Decrement order of those in between
                    $this->_sync(
                        [$orderField => $this->_getUpdateExpression('-')],
                        [
                            $orderField . ' >' => $oldOrder,
                            $orderField . ' <=' => $newOrder,
                        ],
                        $newScope
                    );
                }
            }
        }
    }
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
    {
        $config = $this->getConfig();

        $newOrder = null;
        $newScope = $this->_getScope($entity);
        if ($newScope === false) {
            return;
        }

        $orderField = $config['order'];
        $newOrder = $entity->get($orderField);

        // Adding
        if ($entity->isNew()) {
            // Order not specified
            if ($newOrder === null) {
                // Insert at end of list
                $entity->set($orderField, $this->_getHighestOrder($newScope) + 1);
                // Order specified
            } else {
                // Increment order of records it's inserted before
                $this->_sync(
                    [$orderField => $this->_getUpdateExpression('+')],
                    [$orderField . ' >=' => $newOrder],
                    $newScope
                );
            }

            // Editing
        } else {
            // No action if no new order or scope specified
            if ($newOrder === null && !$newScope) {
                return;
            }

            list($oldOrder, $oldScope) = $this->_getOldValues($entity);

            // No action if new and old scope and order same
            if ($newOrder == $oldOrder &&
                $newScope == $oldScope
            ) {
                return;
            }

            // If changing scope
            if ($newScope && $newScope != $oldScope) {
                // Decrement records in old scope with higher order than moved record old order
                $this->_sync(
                    [$orderField => $this->_getUpdateExpression('-')],
                    [$orderField . ' >' => $oldOrder],
                    $oldScope
                );

                // Order not specified
                if ($newOrder === null) {
                    // Insert at end of new scope
                    $entity->set(
                        $orderField,
                        $this->_getHighestOrder($newScope) + 1
                    );

                    // Order specified
                } else {
                    // Increment records in new scope with higher order than moved record new order
                    $this->_sync(
                        [$orderField => $this->_getUpdateExpression('+')],
                        [$orderField . ' >=' => $newOrder],
                        $newScope
                    );
                }
                // Same scope
            } else {
                // If moving up
                if ($newOrder < $oldOrder) {
                    // Increment order of those in between
                    $this->_sync(
                        [$orderField => $this->_getUpdateExpression('+')],
                        [
                            $orderField . ' >=' => $newOrder,
                            $orderField . ' <' => $oldOrder,
                        ],
                        $newScope
                    );

                // Moving down
                } else {
                    // Decrement order of those in between
                    $this->_sync(
                        [$orderField => $this->_getUpdateExpression('-')],
                        [
                            $orderField . ' >' => $oldOrder,
                            $orderField . ' <=' => $newOrder,
                        ],
                        $newScope
                    );
                }
            }
        }
    }
public function beforeSave ( Event $ event , Entity $ entity , ArrayObject $ options ) { $ config = $ this -> getConfig ( ) ; $ newOrder = null ; $ newScope = $ this -> _getScope ( $ entity ) ; if ( $ newScope === false ) { return ; } $ orderField = $ config [ 'order' ] ; $ newOrder = $ entity -> get ( $ orderField ) ; // Adding if ( $ entity -> isNew ( ) ) { // Order not specified if ( $ newOrder === null ) { // Insert at end of list $ entity -> set ( $ orderField , $ this -> _getHighestOrder ( $ newScope ) + 1 ) ; // Order specified } else { // Increment order of records it's inserted before $ this -> _sync ( [ $ orderField => $ this -> _getUpdateExpression ( '+' ) ] , [ $ orderField . ' >=' => $ newOrder ] , $ newScope ) ; } // Editing } else { // No action if no new order or scope specified if ( $ newOrder === null && ! $ newScope ) { return ; } list ( $ oldOrder , $ oldScope ) = $ this -> _getOldValues ( $ entity ) ; // No action if new and old scope and order same if ( $ newOrder == $ oldOrder && $ newScope == $ oldScope ) { return ; } // If changing scope if ( $ newScope && $ newScope != $ oldScope ) { // Decrement records in old scope with higher order than moved record old order $ this -> _sync ( [ $ orderField => $ this -> _getUpdateExpression ( '-' ) ] , [ $ orderField . ' >' => $ oldOrder ] , $ oldScope ) ; // Order not specified if ( $ newOrder === null ) { // Insert at end of new scope $ entity -> set ( $ orderField , $ this -> _getHighestOrder ( $ newScope ) + 1 ) ; // Order specified } else { // Increment records in new scope with higher order than moved record new order $ this -> _sync ( [ $ orderField => $ this -> _getUpdateExpression ( '+' ) ] , [ $ orderField . ' >=' => $ newOrder ] , $ newScope ) ; } // Same scope } else { // If moving up if ( $ newOrder < $ oldOrder ) { // Increment order of those in between $ this -> _sync ( [ $ orderField => $ this -> _getUpdateExpression ( '+' ) ] , [ $ orderField . ' >=' => $ newOrder , $ orderField . ' <' => $ oldOrder , ] , $ newScope ) ; // Moving down } else { // Decrement order of those in between $ this -> _sync ( [ $ orderField => $ this -> _getUpdateExpression ( '-' ) ] , [ $ orderField . ' >' => $ oldOrder , $ orderField . ' <=' => $ newOrder , ] , $ newScope ) ; } } } }
Sets entity's order and updates order of other records when necessary.

@param \Cake\Event\Event $event The beforeSave event that was fired.
@param \Cake\ORM\Entity $entity The entity that is going to be saved.
@param \ArrayObject $options The options passed to the save method.

@return void
Sets entity s order and updates order of other records when necessary .