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 1 commit
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
82 changes: 76 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;

/**
* Set 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,24 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
*/
class Document
{
private $temp;

/**
* Set file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 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;
}

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

/**
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a space needed here

$this->temp = $this->getAbsolutePath();
}
}

/**
* @ORM\PrePersist()
Expand Down Expand Up @@ -390,23 +452,31 @@ property, instead of the actual filename::
);

unset($this->file);

//check if we have an old image
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use isset here

unlink($this->temp);
}
}

Expand Down