From 2030b282a0c04f44ede076238b71267370b12d21 Mon Sep 17 00:00:00 2001 From: Juan Manuel Date: Wed, 29 Jan 2014 21:59:16 -0200 Subject: [PATCH 1/4] Update file_uploads.rst Code examples do not apply to Doctrine EventSubscribers scenario, on preUpdate. Cheers. --- cookbook/doctrine/file_uploads.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index adb30e27e1f..f8a2113c3c5 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -371,6 +371,12 @@ 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 } } From cc361fe98e0790c85a5c39cbf3f3d30a4d99af3b Mon Sep 17 00:00:00 2001 From: Juan Manuel Date: Tue, 4 Feb 2014 18:12:15 -0200 Subject: [PATCH 2/4] Update file_uploads.rst Updated as per @RyanWeaver's suggestions. --- cookbook/doctrine/file_uploads.rst | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index f8a2113c3c5..683cfec10e6 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -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 } } @@ -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. From 8d00f1a232a34cb865cf16897759e160ed987038 Mon Sep 17 00:00:00 2001 From: Juan Manuel Date: Fri, 21 Mar 2014 17:35:25 -0300 Subject: [PATCH 3/4] Rewording Caution paragraph at line 413 --- cookbook/doctrine/file_uploads.rst | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 683cfec10e6..9a1788fca97 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -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 @@ -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 From d920148e15254eaeabf5c89d0fb321558b203619 Mon Sep 17 00:00:00 2001 From: Juan Manuel Date: Mon, 24 Mar 2014 18:57:40 -0300 Subject: [PATCH 4/4] Going back to the ``Caution`` alternative. --- cookbook/doctrine/file_uploads.rst | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 9a1788fca97..f2bfd1601c1 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -410,22 +410,13 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: } } -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:: - - 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. +.. caution:: + If changes to your entity are handled by a Doctrine event listener or event + subscriber, the ``preUpdate()`` callback must notify Doctrine about the changes + being done. + For full reference on preUpdate event restrictions, see `preUpdate`_ in the + Doctrine Events documentation. 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