-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix the "doctrine file uploads" cookbook examples, see symfony/symfony-d... #2120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8d95a4c
f1bbee6
9ddee15
3c8a816
8b8d35c
53e9b2e
15ccf24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a space needed here |
||
$this->temp = $this->getAbsolutePath(); | ||
} | ||
} | ||
|
||
/** | ||
* @ORM\PrePersist() | ||
|
@@ -390,23 +452,31 @@ property, instead of the actual filename:: | |
); | ||
|
||
unset($this->file); | ||
|
||
//check if we have an old image | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
if (isset($this->temp)) { | ||
// delete the old image | ||
unlink($this->temp); | ||
// clear the temp image path | ||
$this->temp = null; | ||
} | ||
} | ||
|
||
/** | ||
* @ORM\PreRemove() | ||
*/ | ||
public function storeFilenameForRemove() | ||
{ | ||
$this->filenameForRemove = $this->getAbsolutePath(); | ||
$this->temp = $this->getAbsolutePath(); | ||
} | ||
|
||
/** | ||
* @ORM\PostRemove() | ||
*/ | ||
public function removeUpload() | ||
{ | ||
if ($this->filenameForRemove) { | ||
unlink($this->filenameForRemove); | ||
if ($this->temp) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
unlink($this->temp); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
Sets file.