イメージを含む作業エンティティReferences.phpがありますが、Symfony2 でこの参照に保存されている古いイメージを削除し (存在する場合)、新しいイメージを作成する方法がわかりません。現在のイメージは削除されていないため、新しいイメージを作成し、image_path
このエンティティに新しいセットを作成しただけです。メソッドで削除しようとしましたpreUpload
が、現在のファイルをに設定してNULL
から何も設定しません(エラーが発生しました-ファイルを選択する必要があります)
<?php
namespace Acme\ReferenceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity(repositoryClass="Acme\ReferenceBundle\Entity\ReferenceRepository")
* @ORM\Table(name="`references`")
* @ORM\HasLifecycleCallbacks
*/
class Reference
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=200)
* @Assert\NotBlank(
* message = "Name cannot be blank"
* )
* @Assert\Length(
* min = "3",
* minMessage = "Name is too short"
* )
*/
private $name;
/**
* @ORM\Column(type="string", length=200)
* @Assert\NotBlank(
* message = "Description cannot be blank"
* )
* @Assert\Length(
* min = "3",
* minMessage = "Description is too short"
* )
*/
private $description;
/**
* @ORM\Column(type="string", length=200)
* @Assert\Url(
* message = "URL is not valid"
* )
*/
private $url;
/**
* @ORM\ManyToMany(targetEntity="Material", inversedBy="references")
* @Assert\Count(min = 1, minMessage = "Choose any material")
*/
private $materials;
/**
* @ORM\Column(type="text", length=255, nullable=false)
* @Assert\NotNull(
* message = "You have to choose a file"
* )
*/
private $image_path;
/**
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "Max size of file is 5MB.",
* mimeTypesMessage = "There are only allowed jpeg, gif, png and tiff images"
* )
*/
private $file;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Reference
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Reference
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set url
*
* @param string $url
* @return Reference
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set materials
*
* @param string $materials
* @return Reference
*/
public function setMaterials($materials)
{
$this->materials = $materials;
return $this;
}
/**
* Get materials
*
* @return string
*/
public function getMaterials()
{
return $this->materials;
}
/**
* Set image_path
*
* @param string $imagePath
* @return Reference
*/
public function setImagePath($imagePath)
{
$this->image_path = $imagePath;
return $this;
}
/**
* Get image_path
*
* @return string
*/
public function getImagePath()
{
return $this->image_path;
}
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Called before saving the entity
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
$oldImage = $this->image_path;
$oldImagePath = $this->getUploadRootDir().'/'.$oldImage;
if (null !== $this->file) {
if($oldImage && file_exists($oldImagePath)) unlink($oldImagePath); // not working correctly
$filename = sha1(uniqid(mt_rand(), true));
$this->image_path = $filename.'.'.$this->file->guessExtension();
}
}
/**
* Called before entity removal
*
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Called after entity persistence
*
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->file) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->file->move(
$this->getUploadRootDir(),
$this->image_path
);
// set the path property to the filename where you've saved the file
$this->image_path = $this->file->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
protected function getAbsolutePath()
{
return null === $this->image_path
? null
: $this->getUploadRootDir().'/'.$this->image_path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/references';
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->image_path;
}
/**
* Constructor
*/
public function __construct()
{
$this->materials = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add materials
*
* @param \Acme\ReferenceBundle\Entity\Material $materials
* @return Reference
*/
public function addMaterial(\Acme\ReferenceBundle\Entity\Material $materials)
{
$this->materials[] = $materials;
return $this;
}
/**
* Remove materials
*
* @param \Acme\ReferenceBundle\Entity\Material $materials
*/
public function removeMaterial(\Acme\ReferenceBundle\Entity\Material $materials)
{
$this->materials->removeElement($materials);
}
}
何か案が?