src/Model/Behavior/UploadBehavior.php
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
    {
        foreach ($this->getConfig(null, []) as $field => $settings) {
            if (in_array($field, $this->protectedFieldNames)) {
                continue;
            }

            if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) {
                if (Hash::get($settings, 'restoreValueOnFailure', true)) {
                    $entity->set($field, $entity->getOriginal($field));
                    $entity->setDirty($field, false);
                }
                continue;
            }

            $data = $entity->get($field);
            $path = $this->getPathProcessor($entity, $data, $field, $settings);
            $basepath = $path->basepath();
            $filename = $path->filename();
            $data['name'] = $filename;
            $files = $this->constructFiles($entity, $data, $field, $settings, $basepath);

            $writer = $this->getWriter($entity, $data, $field, $settings);
            $success = $writer->write($files);

            if ((new Collection($success))->contains(false)) {
                return false;
            }

            $entity->set($field, $filename);
            $entity->set(Hash::get($settings, 'fields.dir', 'dir'), $basepath);
            $entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']);
            $entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']);
        }
    }
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
    {
        foreach ($this->getConfig(null, []) as $field => $settings) {
            if (in_array($field, $this->protectedFieldNames)) {
                continue;
            }

            if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) {
                if (Hash::get($settings, 'restoreValueOnFailure', true)) {
                    $entity->set($field, $entity->getOriginal($field));
                    $entity->setDirty($field, false);
                }
                continue;
            }

            $data = $entity->get($field);
            $path = $this->getPathProcessor($entity, $data, $field, $settings);
            $basepath = $path->basepath();
            $filename = $path->filename();
            $data['name'] = $filename;
            $files = $this->constructFiles($entity, $data, $field, $settings, $basepath);

            $writer = $this->getWriter($entity, $data, $field, $settings);
            $success = $writer->write($files);

            if ((new Collection($success))->contains(false)) {
                return false;
            }

            $entity->set($field, $filename);
            $entity->set(Hash::get($settings, 'fields.dir', 'dir'), $basepath);
            $entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']);
            $entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']);
        }
    }
public function beforeSave ( Event $ event , Entity $ entity , ArrayObject $ options ) { foreach ( $ this -> getConfig ( null , [ ] ) as $ field => $ settings ) { if ( in_array ( $ field , $ this -> protectedFieldNames ) ) { continue ; } if ( Hash :: get ( ( array ) $ entity -> get ( $ field ) , 'error' ) !== UPLOAD_ERR_OK ) { if ( Hash :: get ( $ settings , 'restoreValueOnFailure' , true ) ) { $ entity -> set ( $ field , $ entity -> getOriginal ( $ field ) ) ; $ entity -> setDirty ( $ field , false ) ; } continue ; } $ data = $ entity -> get ( $ field ) ; $ path = $ this -> getPathProcessor ( $ entity , $ data , $ field , $ settings ) ; $ basepath = $ path -> basepath ( ) ; $ filename = $ path -> filename ( ) ; $ data [ 'name' ] = $ filename ; $ files = $ this -> constructFiles ( $ entity , $ data , $ field , $ settings , $ basepath ) ; $ writer = $ this -> getWriter ( $ entity , $ data , $ field , $ settings ) ; $ success = $ writer -> write ( $ files ) ; if ( ( new Collection ( $ success ) ) -> contains ( false ) ) { return false ; } $ entity -> set ( $ field , $ filename ) ; $ entity -> set ( Hash :: get ( $ settings , 'fields.dir' , 'dir' ) , $ basepath ) ; $ entity -> set ( Hash :: get ( $ settings , 'fields.size' , 'size' ) , $ data [ 'size' ] ) ; $ entity -> set ( Hash :: get ( $ settings , 'fields.type' , 'type' ) , $ data [ 'type' ] ) ; } }
Modifies the entity before it is saved so that uploaded file data is persisted
in the database too.

@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|false
Modifies the entity before it is saved so that uploaded file data is persisted in the database too .