在Symfony中上载文件时,使用Gaufrete捆绑包的优雅方式是什么?
我正在跟踪文件上传,同时使用Gaufrete文件系统 在文档中,文件的移动是通过生命周期回调@PostPersist和@PostUpdate在方法upload()中的文档实体(拥有文件的实体)内进行的。使用@PreRemove和@PostRemove,删除实体时也会自动删除文件在Symfony中上载文件时,使用Gaufrete捆绑包的优雅方式是什么?,symfony,doctrine-orm,Symfony,Doctrine Orm,我正在跟踪文件上传,同时使用Gaufrete文件系统 在文档中,文件的移动是通过生命周期回调@PostPersist和@PostUpdate在方法upload()中的文档实体(拥有文件的实体)内进行的。使用@PreRemove和@PostRemove,删除实体时也会自动删除文件 /** * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class Document { /** * @ORM\PostPersist() * @ORM\PostU
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Document
{
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image and delete
if (isset($this->temp)) {
unlink($this->getUploadRootDir().'/'.$this->temp);
$this->temp = null;
}
$this->file = null;
}
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->temp = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
对于Gaufrete,我必须使用$filesystem->write()方法来移动文件,但据我所知,不建议将Gaufrete服务($filesystem)注入实体(或任何服务)
那么,实现这一点的正确方法是什么呢?您需要使用本问题所述的条令事件订阅服务器:您需要使用本问题所述的条令事件订阅服务器: