Skip to content

Commit

Permalink
Updated after Ryan's review
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed May 21, 2016
1 parent 0031eee commit 1472f3d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions cookbook/controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ controller to specify the directory in which the brochures should be stored:

.. code-block:: yaml
# app/config/parameters.yml
# app/config/config.yml
# ...
parameters:
# ...
brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'
There are some important things to consider in the code of the above controller:
Expand Down Expand Up @@ -320,14 +321,31 @@ automatically upload the file when persisting the entity::
{
$entity = $args->getEntity();

$this->uploadFile($entity);
}

public function preUpdate(LifecycleEventArs $args)
{
$entity = $args->getEntity();

$this->uploadFile($entity);
}

private function uploadFile($entity)
{
// upload only works for Product entities
if (!$entity instanceof Product) {
return;
}

$file = $entity->getBrochure();
$fileName = $this->uploader->upload($file);

// only upload new files
if (!$file instanceof UploadedFile) {
return;
}

$fileName = $this->uploader->upload($file);
$entity->setBrochure($fileName);
}
}
Expand Down

0 comments on commit 1472f3d

Please sign in to comment.