Skip to content

Commit

Permalink
Rewording Caution paragraph at line 413
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmf committed Mar 21, 2014
1 parent 3b0c75d commit 2e21a0b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,22 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
}
}

.. caution::
When using a Doctrine event listener or subscriber, when you make changes
to your entity, the preUpdate() callback must have an extra line of code to
tell Doctrine about the change::

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.
public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();
// do all the file uploading logic
// ...
$entity->setFilename($newFilename);
$args->setNewValue('filename', $newFilename);
}
For full reference on preUpdate event restrictions, see `preUpdate`_ 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
Expand Down Expand Up @@ -556,3 +563,5 @@ You'll notice in this case that you need to do a little bit more work in
order to remove the file. Before it's removed, you must store the file path
(since it depends on the id). Then, once the object has been fully removed
from the database, you can safely delete the file (in ``PostRemove``).

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

0 comments on commit 2e21a0b

Please sign in to comment.