Skip to content

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

Closed
wants to merge 1 commit into from
Closed
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
85 changes: 79 additions & 6 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,27 @@ rules::
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
private $file;

/**
* Set file
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
Copy link
Member

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.

*/
public function setFile($file)
{
$this->file = $file;
}

/**
* Get file
*
* @return \Symfony\Component\HttpFoundation\File\UploadedFile
Copy link
Member

Choose a reason for hiding this comment

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

Samehere

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

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Place a space between // and the text

if ($this->path) {
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

//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.

same here

$this->temp = $this->path;
}
$this->path = NULL;
Copy link
Member

Choose a reason for hiding this comment

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

PHP keywords should be lowercase (null)

}

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

// delete the old image
unlink($this->getUploadRootDir() . '/' . $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.

remove spaces around the . operators

// clear the temp image path
$this->temp = NULL;
Copy link
Member

Choose a reason for hiding this comment

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

lowercase this

}
}

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

at a space between // and the text (on line 423 too)

if (is_file($this->getAbsolutePath())) {
//store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
}
}

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

unset($this->file);

//check if we have an old image
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

// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = NULL;
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}

Expand Down