src/Event/Model/AutoIncrementEventListener.php
public function autoIncrementFieldValue(Event $event, EntityInterface $entity, ArrayObject $options) : void
    {
        $table = $event->getSubject();

        if (!$table instanceof Table) {
            return;
        }

        $fields = $this->getAutoIncrementFields($table);

        // skip if no auto-increment fields are defined
        if (empty($fields)) {
            return;
        }

        // skip modifying auto-increment field(s) on existing records.
        if (! $entity->isNew()) {
            foreach (array_keys($fields) as $field) {
                $entity->unsetProperty((string)$field);
            }

            return;
        }

        foreach ($fields as $field => $options) {
            // get max value
            $query = $table->find('withTrashed');

            try {
                $max = $query->select([$field => $query->func()->max($field)])
                    ->enableHydration(true)
                    ->firstOrFail();
                Assert::isInstanceOf($max, EntityInterface::class);
                $max = (float)$max->get((string)$field);
            } catch (RecordNotFoundException $e) {
                $max = 0;
            }

            if (empty($options['min'])) {
                $entity->set((string)$field, $max + 1);

                continue;
            }

            // if value is less than the allowed minimum, then set it to the minimum.
            $max = $max < $options['min'] ? $options['min'] : $max + 1;

            $entity->set((string)$field, $max);
        }
    }
public function autoIncrementFieldValue(Event $event, EntityInterface $entity, ArrayObject $options) : void
    {
        $table = $event->getSubject();

        if (!$table instanceof Table) {
            return;
        }

        $fields = $this->getAutoIncrementFields($table);

        // skip if no auto-increment fields are defined
        if (empty($fields)) {
            return;
        }

        // skip modifying auto-increment field(s) on existing records.
        if (! $entity->isNew()) {
            foreach (array_keys($fields) as $field) {
                $entity->unsetProperty((string)$field);
            }

            return;
        }

        foreach ($fields as $field => $options) {
            // get max value
            $query = $table->find('withTrashed');

            try {
                $max = $query->select([$field => $query->func()->max($field)])
                    ->enableHydration(true)
                    ->firstOrFail();
                Assert::isInstanceOf($max, EntityInterface::class);
                $max = (float)$max->get((string)$field);
            } catch (RecordNotFoundException $e) {
                $max = 0;
            }

            if (empty($options['min'])) {
                $entity->set((string)$field, $max + 1);

                continue;
            }

            // if value is less than the allowed minimum, then set it to the minimum.
            $max = $max < $options['min'] ? $options['min'] : $max + 1;

            $entity->set((string)$field, $max);
        }
    }
public function autoIncrementFieldValue ( Event $ event , EntityInterface $ entity , ArrayObject $ options ) : void { $ table = $ event -> getSubject ( ) ; if ( ! $ table instanceof Table ) { return ; } $ fields = $ this -> getAutoIncrementFields ( $ table ) ; // skip if no auto-increment fields are defined if ( empty ( $ fields ) ) { return ; } // skip modifying auto-increment field(s) on existing records. if ( ! $ entity -> isNew ( ) ) { foreach ( array_keys ( $ fields ) as $ field ) { $ entity -> unsetProperty ( ( string ) $ field ) ; } return ; } foreach ( $ fields as $ field => $ options ) { // get max value $ query = $ table -> find ( 'withTrashed' ) ; try { $ max = $ query -> select ( [ $ field => $ query -> func ( ) -> max ( $ field ) ] ) -> enableHydration ( true ) -> firstOrFail ( ) ; Assert :: isInstanceOf ( $ max , EntityInterface :: class ) ; $ max = ( float ) $ max -> get ( ( string ) $ field ) ; } catch ( RecordNotFoundException $ e ) { $ max = 0 ; } if ( empty ( $ options [ 'min' ] ) ) { $ entity -> set ( ( string ) $ field , $ max + 1 ) ; continue ; } // if value is less than the allowed minimum, then set it to the minimum. $ max = $ max < $ options [ 'min' ] ? $ options [ 'min' ] : $ max + 1 ; $ entity -> set ( ( string ) $ field , $ max ) ; } }
Auto-increment reference number.

@param  \Cake\Event\Event $event Event object
@param  \Cake\Datasource\EntityInterface $entity Translation entity
@param  \ArrayObject $options entity options
@return void
Auto - increment reference number .