@@ -410,15 +410,22 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
410
410
}
411
411
}
412
412
413
- .. caution ::
413
+ When using a Doctrine event listener or subscriber, when you make changes
414
+ to your entity, the preUpdate() callback must have an extra line of code to
415
+ tell Doctrine about the change::
414
416
415
- When using Doctrine EvensubScribers' preUpdate(\D octrine\C ommon\P ersistence\E vent\P reUpdateEventArgs $args)
416
- callback instead of lifecycle callbacks you also need to invoke, ``$args->setNewValue('path', $filename); ``
417
- as at this point changeSets are not updated and the ``path `` change just made won't have effect
418
- in the data base record.
419
- For full reference on preUpdate event restrictions, see `preUpdate `_ in the in the Doctrine Events documentation.
417
+ public function preUpdate(PreUpdateEventArgs $args)
418
+ {
419
+ $entity = $args->getEntity();
420
+ // do all the file uploading logic
421
+ // ...
422
+ $entity->setFilename($newFilename);
423
+ $args->setNewValue('filename', $newFilename);
424
+ }
425
+
426
+ For full reference on preUpdate event restrictions, see `preUpdate `_ in the
427
+ Doctrine Events documentation.
420
428
421
- .. _`preUpdate` : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate
422
429
423
430
The class now does everything you need: it generates a unique filename before
424
431
persisting, moves the file after persisting, and removes the file if the
@@ -556,3 +563,5 @@ You'll notice in this case that you need to do a little bit more work in
556
563
order to remove the file. Before it's removed, you must store the file path
557
564
(since it depends on the id). Then, once the object has been fully removed
558
565
from the database, you can safely delete the file (in ``PostRemove ``).
566
+
567
+ .. _`preUpdate` : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate
0 commit comments