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

        foreach ($config['fields'] as $field => $fieldOption) {
            $data = $entity->toArray();
            $virtualField = $field . $config['suffix'];

            if (!isset($data[$virtualField]) || !is_array($data[$virtualField])) {
                continue;
            }

            $file = $entity->get($virtualField);

            $error = $this->_triggerErrors($file);

            if ($error === false) {
                continue;
            } elseif (is_string($error)) {
                throw new \ErrorException($error);
            }

            if (!isset($fieldOption['path'])) {
                throw new \LogicException(__('The path for the {0} field is required.', $field));
            }

            if (isset($fieldOption['prefix']) && (is_bool($fieldOption['prefix']) || is_string($fieldOption['prefix']))) {
                $this->_prefix = $fieldOption['prefix'];
            }

            $extension = (new File($file['name'], false))->ext();
            $uploadPath = $this->_getUploadPath($entity, $fieldOption['path'], $extension);
            if (!$uploadPath) {
                throw new \ErrorException(__('Error to get the uploadPath.'));
            }

            $folder = new Folder($this->_config['root']);
            $folder->create($this->_config['root'] . dirname($uploadPath));

            if ($this->_moveFile($entity, $file['tmp_name'], $uploadPath, $field, $fieldOption)) {
                if (!$this->_prefix) {
                    $this->_prefix = '';
                }

                $entity->set($field, $this->_prefix . $uploadPath);
            }

            $entity->unsetProperty($virtualField);
        }
    }
public function beforeSave(Event $event, Entity $entity)
    {
        $config = $this->_config;

        foreach ($config['fields'] as $field => $fieldOption) {
            $data = $entity->toArray();
            $virtualField = $field . $config['suffix'];

            if (!isset($data[$virtualField]) || !is_array($data[$virtualField])) {
                continue;
            }

            $file = $entity->get($virtualField);

            $error = $this->_triggerErrors($file);

            if ($error === false) {
                continue;
            } elseif (is_string($error)) {
                throw new \ErrorException($error);
            }

            if (!isset($fieldOption['path'])) {
                throw new \LogicException(__('The path for the {0} field is required.', $field));
            }

            if (isset($fieldOption['prefix']) && (is_bool($fieldOption['prefix']) || is_string($fieldOption['prefix']))) {
                $this->_prefix = $fieldOption['prefix'];
            }

            $extension = (new File($file['name'], false))->ext();
            $uploadPath = $this->_getUploadPath($entity, $fieldOption['path'], $extension);
            if (!$uploadPath) {
                throw new \ErrorException(__('Error to get the uploadPath.'));
            }

            $folder = new Folder($this->_config['root']);
            $folder->create($this->_config['root'] . dirname($uploadPath));

            if ($this->_moveFile($entity, $file['tmp_name'], $uploadPath, $field, $fieldOption)) {
                if (!$this->_prefix) {
                    $this->_prefix = '';
                }

                $entity->set($field, $this->_prefix . $uploadPath);
            }

            $entity->unsetProperty($virtualField);
        }
    }
public function beforeSave ( Event $ event , Entity $ entity ) { $ config = $ this -> _config ; foreach ( $ config [ 'fields' ] as $ field => $ fieldOption ) { $ data = $ entity -> toArray ( ) ; $ virtualField = $ field . $ config [ 'suffix' ] ; if ( ! isset ( $ data [ $ virtualField ] ) || ! is_array ( $ data [ $ virtualField ] ) ) { continue ; } $ file = $ entity -> get ( $ virtualField ) ; $ error = $ this -> _triggerErrors ( $ file ) ; if ( $ error === false ) { continue ; } elseif ( is_string ( $ error ) ) { throw new \ ErrorException ( $ error ) ; } if ( ! isset ( $ fieldOption [ 'path' ] ) ) { throw new \ LogicException ( __ ( 'The path for the {0} field is required.' , $ field ) ) ; } if ( isset ( $ fieldOption [ 'prefix' ] ) && ( is_bool ( $ fieldOption [ 'prefix' ] ) || is_string ( $ fieldOption [ 'prefix' ] ) ) ) { $ this -> _prefix = $ fieldOption [ 'prefix' ] ; } $ extension = ( new File ( $ file [ 'name' ] , false ) ) -> ext ( ) ; $ uploadPath = $ this -> _getUploadPath ( $ entity , $ fieldOption [ 'path' ] , $ extension ) ; if ( ! $ uploadPath ) { throw new \ ErrorException ( __ ( 'Error to get the uploadPath.' ) ) ; } $ folder = new Folder ( $ this -> _config [ 'root' ] ) ; $ folder -> create ( $ this -> _config [ 'root' ] . dirname ( $ uploadPath ) ) ; if ( $ this -> _moveFile ( $ entity , $ file [ 'tmp_name' ] , $ uploadPath , $ field , $ fieldOption ) ) { if ( ! $ this -> _prefix ) { $ this -> _prefix = '' ; } $ entity -> set ( $ field , $ this -> _prefix . $ uploadPath ) ; } $ entity -> unsetProperty ( $ virtualField ) ; } }
Check if there is some files to upload and modify the entity before
it is saved.

At the end, for each files to upload, unset their "virtual" property.

@param Event  $event  The beforeSave event that was fired.
@param Entity $entity The entity that is going to be saved.

@throws \LogicException When the path configuration is not set.
@throws \ErrorException When the function to get the upload path failed.

@return void
Check if there is some files to upload and modify the entity before it is saved .