public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
    {
        $association = $this->versionAssociation();
        $name = $association->getName();
        $newOptions = [$name => ['validate' => false]];
        $options['associated'] = $newOptions + $options['associated'];

        $fields = $this->_fields();
        $values = $entity->extract($fields, $this->_config['onlyDirty']);

        $primaryKey = (array)$this->_table->getPrimaryKey();
        $versionField = $this->_config['versionField'];

        if (isset($options['versionId'])) {
            $versionId = $options['versionId'];
        } else {
            $versionId = $this->getVersionId($entity) + 1;
        }
        $created = new DateTime();
        $new = [];
        $entityClass = TableRegistry::get($this->_config['versionTable'])->getEntityClass();
        foreach ($values as $field => $content) {
            if (in_array($field, $primaryKey) || $field == $versionField) {
                continue;
            }

            $converted = $this->_convertFieldsToType([$field => $content], 'toDatabase');

            $data = [
                'version_id' => $versionId,
                'model' => $this->_config['referenceName'],
                'field' => $field,
                'content' => $converted[$field],
                'created' => $created,
            ] + $this->_extractForeignKey($entity);

            $event = new Event('Model.Version.beforeSave', $this, $options);
            $userData = EventManager::instance()->dispatch($event);
            if (isset($userData->result) && is_array($userData->result)) {
                $data = array_merge($data, $userData->result);
            }

            $new[$field] = new $entityClass($data, [
                'useSetters' => false,
                'markNew' => true
            ]);
        }

        $entity->set($association->getProperty(), $new);
        if (!empty($versionField) && in_array($versionField, $this->_table->getSchema()->columns())) {
            $entity->set($this->_config['versionField'], $versionId);
        }
    }
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
    {
        $association = $this->versionAssociation();
        $name = $association->getName();
        $newOptions = [$name => ['validate' => false]];
        $options['associated'] = $newOptions + $options['associated'];

        $fields = $this->_fields();
        $values = $entity->extract($fields, $this->_config['onlyDirty']);

        $primaryKey = (array)$this->_table->getPrimaryKey();
        $versionField = $this->_config['versionField'];

        if (isset($options['versionId'])) {
            $versionId = $options['versionId'];
        } else {
            $versionId = $this->getVersionId($entity) + 1;
        }
        $created = new DateTime();
        $new = [];
        $entityClass = TableRegistry::get($this->_config['versionTable'])->getEntityClass();
        foreach ($values as $field => $content) {
            if (in_array($field, $primaryKey) || $field == $versionField) {
                continue;
            }

            $converted = $this->_convertFieldsToType([$field => $content], 'toDatabase');

            $data = [
                'version_id' => $versionId,
                'model' => $this->_config['referenceName'],
                'field' => $field,
                'content' => $converted[$field],
                'created' => $created,
            ] + $this->_extractForeignKey($entity);

            $event = new Event('Model.Version.beforeSave', $this, $options);
            $userData = EventManager::instance()->dispatch($event);
            if (isset($userData->result) && is_array($userData->result)) {
                $data = array_merge($data, $userData->result);
            }

            $new[$field] = new $entityClass($data, [
                'useSetters' => false,
                'markNew' => true
            ]);
        }

        $entity->set($association->getProperty(), $new);
        if (!empty($versionField) && in_array($versionField, $this->_table->getSchema()->columns())) {
            $entity->set($this->_config['versionField'], $versionId);
        }
    }
public function beforeSave ( Event $ event , EntityInterface $ entity , ArrayObject $ options ) { $ association = $ this -> versionAssociation ( ) ; $ name = $ association -> getName ( ) ; $ newOptions = [ $ name => [ 'validate' => false ] ] ; $ options [ 'associated' ] = $ newOptions + $ options [ 'associated' ] ; $ fields = $ this -> _fields ( ) ; $ values = $ entity -> extract ( $ fields , $ this -> _config [ 'onlyDirty' ] ) ; $ primaryKey = ( array ) $ this -> _table -> getPrimaryKey ( ) ; $ versionField = $ this -> _config [ 'versionField' ] ; if ( isset ( $ options [ 'versionId' ] ) ) { $ versionId = $ options [ 'versionId' ] ; } else { $ versionId = $ this -> getVersionId ( $ entity ) + 1 ; } $ created = new DateTime ( ) ; $ new = [ ] ; $ entityClass = TableRegistry :: get ( $ this -> _config [ 'versionTable' ] ) -> getEntityClass ( ) ; foreach ( $ values as $ field => $ content ) { if ( in_array ( $ field , $ primaryKey ) || $ field == $ versionField ) { continue ; } $ converted = $ this -> _convertFieldsToType ( [ $ field => $ content ] , 'toDatabase' ) ; $ data = [ 'version_id' => $ versionId , 'model' => $ this -> _config [ 'referenceName' ] , 'field' => $ field , 'content' => $ converted [ $ field ] , 'created' => $ created , ] + $ this -> _extractForeignKey ( $ entity ) ; $ event = new Event ( 'Model.Version.beforeSave' , $ this , $ options ) ; $ userData = EventManager :: instance ( ) -> dispatch ( $ event ) ; if ( isset ( $ userData -> result ) && is_array ( $ userData -> result ) ) { $ data = array_merge ( $ data , $ userData -> result ) ; } $ new [ $ field ] = new $ entityClass ( $ data , [ 'useSetters' => false , 'markNew' => true ] ) ; } $ entity -> set ( $ association -> getProperty ( ) , $ new ) ; if ( ! empty ( $ versionField ) && in_array ( $ versionField , $ this -> _table -> getSchema ( ) -> columns ( ) ) ) { $ entity -> set ( $ this -> _config [ 'versionField' ] , $ versionId ) ; } }
Modifies the entity before it is saved so that versioned fields are persisted
in the database too.

@param \Cake\Event\Event $event The beforeSave event that was fired
@param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved
@param \ArrayObject $options the options passed to the save method
@return void
Modifies the entity before it is saved so that versioned fields are persisted in the database too .