1

ある種のファイルが添付された複数のエンティティがあります。doctrine を使用してファイルのアップロードを処理する方法に関するクックブックのエントリ ( here )に従いましたが、私の質問は次のとおりです。

多くのコードを複製することなく、複数のエンティティに対してこれを行うにはどうすればよいですか? 現在、私のすべてのエンティティは、パスの取得に関連する関数を共有していますが、あちこちに変更が加えられているため、同一ではありませんが、ほとんどのコードは同一です。これを行う最もクリーンな方法は何ですか?

2 つのエンティティの例を次に示します。重複する機能に注意してください。

画像:

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

エンティティを渡すことができるメソッドを使用してアップロード サービス クラスを個人的に作成します。必要な場所にこのサービスを挿入し、1 か所だけに記述できます。一般に、エンティティにこの種のロジックを含めることは良い考えではありません。

この場合の継承は、一般化ではなく動作を追加する方法であるため、私の観点からは完全に間違っているように見えます。それを行うこともできますが、私はそれに対してアドバイスします。継承自体は使いすぎであり、最もクリーンな方法ではありません。また、Doctrine での継承の実装も非常に面倒です。特性はそれを行うことができますが、この種の環境ではまだあまり経験がありません. Symfony が意図された方法でサービスを使用することを強くお勧めします。

于 2012-12-31T14:09:06.737 に答える