From 8d95a4c5de57d16e3b31fc10d64a82ab4a8c65dc Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Thu, 10 Jan 2013 22:03:03 +0200 Subject: [PATCH 1/7] Fix the "doctrine file uploads" cookbook examples, see symfony/symfony-docs#600 --- cookbook/doctrine/file_uploads.rst | 82 +++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 287f8104459..19bcd2545c6 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -122,13 +122,35 @@ rules:: // src/Acme/DemoBundle/Entity/Document.php + use Symfony\Component\HttpFoundation\File\UploadedFile; + // ... class Document { /** * @Assert\File(maxSize="6000000") */ - public $file; + private $file; + + /** + * Set file + * + * @param UploadedFile $file + */ + public function setFile(UploadedFile $file) + { + $this->file = $file; + } + + /** + * Get file + * + * @return UploadedFile + */ + public function getFile() + { + return $this->file; + } // ... } @@ -268,6 +290,24 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: */ class Document { + private $temp; + + /** + * Set file + * + * @param UploadedFile $file + */ + public function setFile(UploadedFile $file) + { + $this->file = $file; + // check if we have an old image path + if (isset($this->path)) { + // store the old name to delete after the update + $this->temp = $this->path; + } + $this->path = null; + } + /** * @ORM\PrePersist() * @ORM\PreUpdate() @@ -297,6 +337,14 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: $this->file->move($this->getUploadRootDir(), $this->path); unset($this->file); + + //check if we have an old image + if (isset($this->temp)) { + // delete the old image + unlink($this->getUploadRootDir().'/'.$this->temp); + // clear the temp image path + $this->temp = null; + } } /** @@ -357,8 +405,22 @@ property, instead of the actual filename:: */ class Document { - // a property used temporarily while deleting - private $filenameForRemove; + private $temp; + + /** + * Set file + * + * @param UploadedFile $file + */ + public function setFile(UploadedFile $file) + { + $this->file = $file; + // check if we have an old image path + if (is_file($this->getAbsolutePath())) { + //store the old name to delete after the update + $this->temp = $this->getAbsolutePath(); + } + } /** * @ORM\PrePersist() @@ -390,6 +452,14 @@ property, instead of the actual filename:: ); unset($this->file); + + //check if we have an old image + if (isset($this->temp)) { + // delete the old image + unlink($this->temp); + // clear the temp image path + $this->temp = null; + } } /** @@ -397,7 +467,7 @@ property, instead of the actual filename:: */ public function storeFilenameForRemove() { - $this->filenameForRemove = $this->getAbsolutePath(); + $this->temp = $this->getAbsolutePath(); } /** @@ -405,8 +475,8 @@ property, instead of the actual filename:: */ public function removeUpload() { - if ($this->filenameForRemove) { - unlink($this->filenameForRemove); + if ($this->temp) { + unlink($this->temp); } } From f1bbee63c65c60b38b7e750c96cdd94a48ad14d4 Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Fri, 11 Jan 2013 13:55:14 +0200 Subject: [PATCH 2/7] cs fixes --- cookbook/doctrine/file_uploads.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 19bcd2545c6..2a3d0481792 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -133,7 +133,7 @@ rules:: private $file; /** - * Set file + * Sets file * * @param UploadedFile $file */ @@ -417,7 +417,7 @@ property, instead of the actual filename:: $this->file = $file; // check if we have an old image path if (is_file($this->getAbsolutePath())) { - //store the old name to delete after the update + // store the old name to delete after the update $this->temp = $this->getAbsolutePath(); } } @@ -453,7 +453,7 @@ property, instead of the actual filename:: unset($this->file); - //check if we have an old image + // check if we have an old image if (isset($this->temp)) { // delete the old image unlink($this->temp); @@ -475,7 +475,7 @@ property, instead of the actual filename:: */ public function removeUpload() { - if ($this->temp) { + if (isset($this->temp)) { unlink($this->temp); } } From 9ddee15a668422642763769aa132455dd3676a30 Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Fri, 11 Jan 2013 13:57:27 +0200 Subject: [PATCH 3/7] more cs fixes --- cookbook/doctrine/file_uploads.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 2a3d0481792..ce3627d151b 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -293,7 +293,7 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: private $temp; /** - * Set file + * Sets file * * @param UploadedFile $file */ @@ -408,7 +408,7 @@ property, instead of the actual filename:: private $temp; /** - * Set file + * Sets file * * @param UploadedFile $file */ From 3c8a8162317333df6b656d939421862ab4966722 Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Fri, 11 Jan 2013 14:58:40 +0200 Subject: [PATCH 4/7] add missing space --- cookbook/doctrine/file_uploads.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index ce3627d151b..81b923756d6 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -338,7 +338,7 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: unset($this->file); - //check if we have an old image + // check if we have an old image if (isset($this->temp)) { // delete the old image unlink($this->getUploadRootDir().'/'.$this->temp); From 8b8d35c518557d44c04b1e0e48f5c658f3986f56 Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Sat, 12 Jan 2013 11:29:32 +0200 Subject: [PATCH 5/7] end short descriptions in PHPdoc comment with a . --- cookbook/doctrine/file_uploads.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 81b923756d6..641235d56cc 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -133,7 +133,7 @@ rules:: private $file; /** - * Sets file + * Sets file. * * @param UploadedFile $file */ @@ -143,7 +143,7 @@ rules:: } /** - * Get file + * Get file. * * @return UploadedFile */ @@ -293,7 +293,7 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: private $temp; /** - * Sets file + * Sets file. * * @param UploadedFile $file */ @@ -408,7 +408,7 @@ property, instead of the actual filename:: private $temp; /** - * Sets file + * Sets file. * * @param UploadedFile $file */ From 53e9b2e43e83415c68e4458a5d0648b3b2196802 Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Mon, 11 Feb 2013 22:47:08 +0200 Subject: [PATCH 6/7] fix delete the old image before the move of new image because of the same name --- cookbook/doctrine/file_uploads.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 641235d56cc..df6e5113627 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -443,6 +443,14 @@ property, instead of the actual filename:: return; } + // check if we have an old image + if (isset($this->temp)) { + // delete the old image + unlink($this->temp); + // clear the temp image path + $this->temp = null; + } + // you must throw an exception here if the file cannot be moved // so that the entity is not persisted to the database // which the UploadedFile move() method does @@ -452,14 +460,6 @@ property, instead of the actual filename:: ); unset($this->file); - - // check if we have an old image - if (isset($this->temp)) { - // delete the old image - unlink($this->temp); - // clear the temp image path - $this->temp = null; - } } /** From 15ccf24a60db0491bacba9886d8329efe87e20cb Mon Sep 17 00:00:00 2001 From: Mahmoud Mostafa Date: Sat, 23 Mar 2013 22:11:51 +0200 Subject: [PATCH 7/7] fix a case when you set only the file field for the first time --- cookbook/doctrine/file_uploads.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index df6e5113627..e7397e8d08f 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -304,8 +304,10 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: if (isset($this->path)) { // store the old name to delete after the update $this->temp = $this->path; + $this->path = null; + } else { + $this->path = 'initial'; } - $this->path = null; } /** @@ -419,6 +421,8 @@ property, instead of the actual filename:: if (is_file($this->getAbsolutePath())) { // store the old name to delete after the update $this->temp = $this->getAbsolutePath(); + } else { + $this->path = 'initial'; } }