ファイルのアップロードを処理する必要がある単純な Doctrine Entity があります。このエンティティを実装するために Symfony の推奨事項に従っています ( http://symfony.com/doc/current/cookbook/doctrine/file_uploads.htmlを参照)。したがって、次のようなエンティティがあります (簡略化)。
<?php
// src/Acme/DemoBundle/Entity/Document.php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*/
class Document
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
public $name;
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$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/documents';
}
}
次のように、Symfony 構成ファイルでアップロード ディレクトリを構成可能にすることで、さらに一歩進めたいと思います。
parameters:
upload_dir: new_upload_dir_value
次に、次の代わりに、エンティティでこの構成値を使用します。
- メソッドの現在のファイルに基づいてアップロードディレクトリを推測します
getUploadRootDir
(エンティティファイルの場所を変更するとどうなりますか?) "uploads"
文字列をメソッドに直接入れますgetUploadDir
(いつでも名前を変更できるようにしたいです)
私は Symfony2 を初めて使用するので、このケースを処理するための適切なアプローチを持っていない可能性がありますが、多くの欠点なしでこれを処理する良い方法を知っている人はいますか?