Skip to content

Commit

Permalink
Update file_uploads.rst
Browse files Browse the repository at this point in the history
Updated as per @RyanWeaver's suggestions.
  • Loading branch information
juanmf authored and weaverryan committed Mar 27, 2014
1 parent 2030b28 commit cc361fe
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,6 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->getFile()->guessExtension();
// Note: is you choose to use EvensubScribers, this change has no effect un database Update
// the 'initial' value set in {@link self::setFile()} is used instead. As this occurs
// inside EntityManager#flush() and changeSets are not tracked.
// use \Doctrine\Common\Persistence\Event\PreUpdateEventArgs $args as the handler fn param and
// $args->setNewValue('path', $filename);
// source: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate
}
}

Expand Down Expand Up @@ -416,6 +410,16 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
}
}

.. caution::

When using Doctrine EvensubScribers' preUpdate(\Doctrine\Common\Persistence\Event\PreUpdateEventArgs $args)
callback instead of lifecycle callbacks you also need to invoke, ``$args->setNewValue('path', $filename);``
as at this point changeSets are not updated and the ``path`` change just made won't have effect
in the data base record.
For full reference on preUpdate event restrictions, see `preUpdate`_ in the in the Doctrine Events documentation.

.. _`preUpdate`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate

The class now does everything you need: it generates a unique filename before
persisting, moves the file after persisting, and removes the file if the
entity is ever deleted.
Expand Down

0 comments on commit cc361fe

Please sign in to comment.