public function setOrder(array $records)
    {
        $config = $this->getConfig();
        $table = $this->_table;

        $table->removeBehavior('Sequence');

        $return = $table->getConnection()->transactional(
            function ($connection) use ($table, $records) {
                $order = $this->_config['start'];
                $field = $this->_config['order'];

                /** @var string $primaryKeyField */
                $primaryKeyField = $table->getPrimaryKey();
                foreach ($records as $record) {
                    if (is_scalar($record)) {
                        $record = [$primaryKeyField => $record];
                    }

                    if (is_array($record)) {
                        $record = $table->newEntity($record, [
                            'fields' => array_keys($record),
                            'validate' => false,
                            'accessibleFields' => [
                                $primaryKeyField => true,
                            ],
                        ]);
                        $record->isNew(false);
                        $record->setDirty($primaryKeyField, false);
                    }

                    $record->setAccess($field, true);
                    $record->set($field, $order++);

                    $r = $table->save(
                        $record,
                        ['atomic' => false, 'checkRules' => false]
                    );
                    if ($r === false) {
                        return false;
                    }
                }

                return true;
            }
        );

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

        return $return;
    }
public function setOrder(array $records)
    {
        $config = $this->getConfig();
        $table = $this->_table;

        $table->removeBehavior('Sequence');

        $return = $table->getConnection()->transactional(
            function ($connection) use ($table, $records) {
                $order = $this->_config['start'];
                $field = $this->_config['order'];

                /** @var string $primaryKeyField */
                $primaryKeyField = $table->getPrimaryKey();
                foreach ($records as $record) {
                    if (is_scalar($record)) {
                        $record = [$primaryKeyField => $record];
                    }

                    if (is_array($record)) {
                        $record = $table->newEntity($record, [
                            'fields' => array_keys($record),
                            'validate' => false,
                            'accessibleFields' => [
                                $primaryKeyField => true,
                            ],
                        ]);
                        $record->isNew(false);
                        $record->setDirty($primaryKeyField, false);
                    }

                    $record->setAccess($field, true);
                    $record->set($field, $order++);

                    $r = $table->save(
                        $record,
                        ['atomic' => false, 'checkRules' => false]
                    );
                    if ($r === false) {
                        return false;
                    }
                }

                return true;
            }
        );

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

        return $return;
    }
public function setOrder ( array $ records ) { $ config = $ this -> getConfig ( ) ; $ table = $ this -> _table ; $ table -> removeBehavior ( 'Sequence' ) ; $ return = $ table -> getConnection ( ) -> transactional ( function ( $ connection ) use ( $ table , $ records ) { $ order = $ this -> _config [ 'start' ] ; $ field = $ this -> _config [ 'order' ] ; /** @var string $primaryKeyField */ $ primaryKeyField = $ table -> getPrimaryKey ( ) ; foreach ( $ records as $ record ) { if ( is_scalar ( $ record ) ) { $ record = [ $ primaryKeyField => $ record ] ; } if ( is_array ( $ record ) ) { $ record = $ table -> newEntity ( $ record , [ 'fields' => array_keys ( $ record ) , 'validate' => false , 'accessibleFields' => [ $ primaryKeyField => true , ] , ] ) ; $ record -> isNew ( false ) ; $ record -> setDirty ( $ primaryKeyField , false ) ; } $ record -> setAccess ( $ field , true ) ; $ record -> set ( $ field , $ order ++ ) ; $ r = $ table -> save ( $ record , [ 'atomic' => false , 'checkRules' => false ] ) ; if ( $ r === false ) { return false ; } } return true ; } ) ; $ table -> addBehavior ( 'ADmad/Sequence.Sequence' , $ config ) ; return $ return ; }
Set order for list of records provided.

Records can be provided as array of entities or array of associative
arrays like `[['id' => 1], ['id' => 2]]` or array of primary key values
like `[1, 2]`.

@param array $records Records.

@return bool
Set order for list of records provided .