-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
fix the "doctrine file uploads" cookbook examples #2118
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 all commits
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 |
---|---|---|
|
@@ -128,7 +128,27 @@ rules:: | |
/** | ||
* @Assert\File(maxSize="6000000") | ||
*/ | ||
public $file; | ||
private $file; | ||
|
||
/** | ||
* Set file | ||
* | ||
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file | ||
*/ | ||
public function setFile($file) | ||
{ | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* Get file | ||
* | ||
* @return \Symfony\Component\HttpFoundation\File\UploadedFile | ||
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. Samehere |
||
*/ | ||
public function getFile() | ||
{ | ||
return $this->file; | ||
} | ||
|
||
// ... | ||
} | ||
|
@@ -268,6 +288,29 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: | |
*/ | ||
class Document | ||
{ | ||
/** | ||
* a temp variable for storing the old image name | ||
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. Should begin with an uppercase letter and end with a dot. |
||
* used to delete the old image when updating the image field | ||
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. There should be an empty line around this 'long description' and you should make sentences, with capitalized sentence, dots and commas. |
||
* @var string $temp | ||
*/ | ||
private $temp; | ||
|
||
/** | ||
* Set file | ||
* | ||
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file | ||
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. See my first comment |
||
*/ | ||
public function setFile($file) | ||
{ | ||
$this->file = $file; | ||
//check if we have an old image path | ||
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. Place a space between |
||
if ($this->path) { | ||
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 |
||
//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. same here |
||
$this->temp = $this->path; | ||
} | ||
$this->path = NULL; | ||
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. PHP keywords should be lowercase ( |
||
} | ||
|
||
/** | ||
* @ORM\PrePersist() | ||
* @ORM\PreUpdate() | ||
|
@@ -297,6 +340,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 ($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 |
||
// delete the old image | ||
unlink($this->getUploadRootDir() . '/' . $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. remove spaces around the |
||
// clear the temp image path | ||
$this->temp = NULL; | ||
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. lowercase this |
||
} | ||
} | ||
|
||
/** | ||
|
@@ -357,8 +408,22 @@ property, instead of the actual filename:: | |
*/ | ||
class Document | ||
{ | ||
// a property used temporarily while deleting | ||
private $filenameForRemove; | ||
private $temp; | ||
|
||
/** | ||
* Set file | ||
* | ||
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file | ||
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. see first comment |
||
*/ | ||
public function setFile($file) | ||
{ | ||
$this->file = $file; | ||
//check if we have an old image path | ||
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. at a space between |
||
if (is_file($this->getAbsolutePath())) { | ||
//store the old name to delete after the update | ||
$this->temp = $this->getAbsolutePath(); | ||
} | ||
} | ||
|
||
/** | ||
* @ORM\PrePersist() | ||
|
@@ -390,23 +455,31 @@ property, instead of the actual filename:: | |
); | ||
|
||
unset($this->file); | ||
|
||
//check if we have an old image | ||
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 |
||
// delete the old image | ||
unlink($this->temp); | ||
// clear the temp image path | ||
$this->temp = NULL; | ||
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. lowercase 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) { | ||
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.
You should add this namespace in a
use
statement in the beginning of the code example, remove the namespace here and add a typehint for the PHP parameter.