Skip to content

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

Merged
merged 7 commits into from
Mar 30, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file)
{
$this->file = $file;
}

/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}

// ...
}
Expand Down Expand Up @@ -268,6 +290,26 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
*/
class Document
{
private $temp;

/**
* Sets 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;
} else {
$this->path = 'initial';
}
}

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
Expand Down Expand Up @@ -297,6 +339,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;
}
}

/**
Expand Down Expand Up @@ -357,8 +407,24 @@ property, instead of the actual filename::
*/
class Document
{
// a property used temporarily while deleting
private $filenameForRemove;
private $temp;

/**
* Sets 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();
} else {
$this->path = 'initial';
}
}

/**
* @ORM\PrePersist()
Expand All @@ -381,6 +447,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
Expand All @@ -397,16 +471,16 @@ property, instead of the actual filename::
*/
public function storeFilenameForRemove()
{
$this->filenameForRemove = $this->getAbsolutePath();
$this->temp = $this->getAbsolutePath();
}

/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($this->filenameForRemove) {
unlink($this->filenameForRemove);
if (isset($this->temp)) {
unlink($this->temp);
}
}

Expand Down