1

我有多个实体附有某种文件。我遵循了关于如何使用学说(此处)处理文件上传的食谱条目,但我的问题是:

如何在不复制大量代码的情况下为多个实体执行此操作?现在我的所有实体都共享与获取路径相关的功能,尽管在这里和那里进行了一些修改,所以它们并不相同,但大部分代码都是。这样做最干净的方法是什么?

这是两个实体的示例,请注意重叠功能:

图片:

public function preUpload()
{
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();

    if (!is_dir($dir)) {
        mkdir($dir);
    }

    $slugger = new \Sam\TourBundle\Service\SlugService();

    $pathinfo = pathinfo($this->file->getClientOriginalName());
    $path = $slugger->slugize($pathinfo['filename']).'.'.$pathinfo['extension'];
    $this->path = $path;

    // If file already exists rename it
    if (file_exists($this->getAbsolutePath())) {
        $i = 1;
        while (file_exists($this->getAbsolutePath())) {
            $this->path = $i.'-'.$this->path;
            $i++;
        }
    }

}

/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();
    $this->file->move($dir, $this->path);

    $thumb = new \Sam\TourBundle\Service\ThumbnailService();
    $thumb->makeThumbnail($this->getAbsolutePath());

    unset($this->file);
}

/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
    $file = $this->getAbsolutePath();
    $thumb_file = $this->getThumbnailAbsolutePath();

    if (file_exists($file)) {
        unlink($file);
    }

    if (file_exists($thumb_file)) {
        unlink($thumb_file);
    }
}

public function getParentDir()
{
    return $galerija = $this->getGalerija()->getSlug().'/';
    //return null === $galerija ? null : $this->getUploadRootDir().'/'.$galerija.$this->path;
}

public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->getParentDir().$this->path;
}

public function getThumbnailAbsolutePath()
{
    $pathinfo = pathinfo($this->getAbsolutePath());
    $thumbpath = $pathinfo['dirname'].'/'.$pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
    return null === $this->path ? null : $thumbpath;
}

public function getRootDir()
{
    return __DIR__;
}

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->getParentDir().$this->path;
}

public function getThumbnailWebPath()
{
    $pathinfo = pathinfo($this->getAbsolutePath());
    $thumbpath = $pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->getParentDir().$thumbpath;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return realpath(__DIR__.'/../../../../web/'.$this->getUploadDir());
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view
    return 'images';
}

文档:

public function preUpload()
{
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();

    if (!is_dir($dir)) {
        mkdir($dir);
    }

    $slugger = new \Sam\TourBundle\Service\SlugService();

    $pathinfo = pathinfo($this->file->getClientOriginalName());
    $path = $slugger->slugize($pathinfo['filename']).'.'.$pathinfo['extension'];
    $this->path = $path;

    // If file already exists rename it
    if (file_exists($this->getAbsolutePath())) {
        $i = 1;
        while (file_exists($this->getAbsolutePath())) {
            $this->path = $i.'-'.$this->path;
            $i++;
        }
    }

}

/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();
    $this->file->move($dir, $this->path);

    unset($this->file);
}

/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
    $file = $this->getAbsolutePath();

    if (file_exists($file)) {
        unlink($file);
    }
}

public function getParentDir()
{
    return $ponuda = $this->getPonuda()->getSlug().'/';
}

public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->getParentDir().$this->path;
}

public function getRootDir()
{
    return __DIR__;
}

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->getParentDir().$this->path;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return realpath(__DIR__.'/../../../../web/'.$this->getUploadDir());
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view
    return 'documents';
}
4

1 回答 1

0

我会亲自创建一个上传服务类,其中包含一个可以传递实体的方法,您可以在需要的任何地方注入此服务,并且只将它写在一个地方。我相信,在您的实体中拥有这种逻辑通常不是一个好主意。

从我的角度来看,这种情况下的继承看起来完全不正确,因为它是一种添加行为而不是泛化的方式,你可以这样做,但我建议不要这样做。继承本身被过度使用,而不是最干净的工作方式,而且 Doctrine 的实现也很烦人。Traits 可以做到这一点,尽管在这种环境下我还没有太多使用它们的经验。我强烈建议按照 Symfony 的使用方式使用服务。

于 2012-12-31T14:09:06.737 回答