Skip to content

Commit 8d95a4c

Browse files
committed
Fix the "doctrine file uploads" cookbook examples, see #600
1 parent a7cba83 commit 8d95a4c

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

cookbook/doctrine/file_uploads.rst

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,35 @@ rules::
122122

123123
// src/Acme/DemoBundle/Entity/Document.php
124124

125+
use Symfony\Component\HttpFoundation\File\UploadedFile;
126+
125127
// ...
126128
class Document
127129
{
128130
/**
129131
* @Assert\File(maxSize="6000000")
130132
*/
131-
public $file;
133+
private $file;
134+
135+
/**
136+
* Set file
137+
*
138+
* @param UploadedFile $file
139+
*/
140+
public function setFile(UploadedFile $file)
141+
{
142+
$this->file = $file;
143+
}
144+
145+
/**
146+
* Get file
147+
*
148+
* @return UploadedFile
149+
*/
150+
public function getFile()
151+
{
152+
return $this->file;
153+
}
132154

133155
// ...
134156
}
@@ -268,6 +290,24 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
268290
*/
269291
class Document
270292
{
293+
private $temp;
294+
295+
/**
296+
* Set file
297+
*
298+
* @param UploadedFile $file
299+
*/
300+
public function setFile(UploadedFile $file)
301+
{
302+
$this->file = $file;
303+
// check if we have an old image path
304+
if (isset($this->path)) {
305+
// store the old name to delete after the update
306+
$this->temp = $this->path;
307+
}
308+
$this->path = null;
309+
}
310+
271311
/**
272312
* @ORM\PrePersist()
273313
* @ORM\PreUpdate()
@@ -297,6 +337,14 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
297337
$this->file->move($this->getUploadRootDir(), $this->path);
298338

299339
unset($this->file);
340+
341+
//check if we have an old image
342+
if (isset($this->temp)) {
343+
// delete the old image
344+
unlink($this->getUploadRootDir().'/'.$this->temp);
345+
// clear the temp image path
346+
$this->temp = null;
347+
}
300348
}
301349

302350
/**
@@ -357,8 +405,22 @@ property, instead of the actual filename::
357405
*/
358406
class Document
359407
{
360-
// a property used temporarily while deleting
361-
private $filenameForRemove;
408+
private $temp;
409+
410+
/**
411+
* Set file
412+
*
413+
* @param UploadedFile $file
414+
*/
415+
public function setFile(UploadedFile $file)
416+
{
417+
$this->file = $file;
418+
// check if we have an old image path
419+
if (is_file($this->getAbsolutePath())) {
420+
//store the old name to delete after the update
421+
$this->temp = $this->getAbsolutePath();
422+
}
423+
}
362424

363425
/**
364426
* @ORM\PrePersist()
@@ -390,23 +452,31 @@ property, instead of the actual filename::
390452
);
391453

392454
unset($this->file);
455+
456+
//check if we have an old image
457+
if (isset($this->temp)) {
458+
// delete the old image
459+
unlink($this->temp);
460+
// clear the temp image path
461+
$this->temp = null;
462+
}
393463
}
394464

395465
/**
396466
* @ORM\PreRemove()
397467
*/
398468
public function storeFilenameForRemove()
399469
{
400-
$this->filenameForRemove = $this->getAbsolutePath();
470+
$this->temp = $this->getAbsolutePath();
401471
}
402472

403473
/**
404474
* @ORM\PostRemove()
405475
*/
406476
public function removeUpload()
407477
{
408-
if ($this->filenameForRemove) {
409-
unlink($this->filenameForRemove);
478+
if ($this->temp) {
479+
unlink($this->temp);
410480
}
411481
}
412482

0 commit comments

Comments
 (0)