php – 如何将文件上传到Symfony2 DataFixture?
|
我似乎无法围绕我如何将文件上传添加到DataFixture中.我正在尝试上传一个图像为虚拟内容我的装置加载.这似乎是有用的知识. 虽然这个问题在1年前已经被问到,但似乎没有很多关于如何通过教学数据夹具上传文件的信息.我只能找到这个帖子. 我一直在寻找,我已经采取了一个略有不同的方法,而不是华丽的. (可能与Symfony的更新有关.) 我第一次不得不 use SymfonyComponentHttpFoundationFileUploadedFile; 然后使用copy()来复制图像,因为Ornj表示将会移动它. copy($art1->getFixturesPath() . '01.jpg',$art1->getFixturesPath() . '01-copy.jpg'); 然后使用以下命令创建并添加文件: $file = new UploadedFile($art1->getFixturesPath() . '01-copy.jpg','Image1',null,true); $art1->setFile($file); $manager->persist($art1); 如果在”UploadedFile’构造函数中没有将最后一个参数设置为’true”,因为在运行”doctrine:fixtures:load”时会抛出一个未知错误. 方法“getFixturesPath()”只是检索存储样本图像的路径: // Entity file
public function getFixturesPath()
{
return $this->getAbsolutePath() . 'web/uploads/art/fixtures/';
}
“getAbsolutePath()”方法已经从Doctrine File Uploads开始. 完整的工作代码: <?php
//src/User/MyBundle/Entity/Art.php
namespace User/MyBundle/Entity;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileUploadedFile;
use SymfonyComponentValidatorConstraints as Assert;
/**
*
* Art Entity
*
* @ORMEntity(repositoryClass="UserMyBundleEntityRepositoryArtRepository")
* @ORMTable(name="art")
* @ORMHasLifecycleCallbacks
*/
class Art
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string",length=100)
*/
protected $title;
/**
* @ORMColumn(type="string",length=255,nullable=true)
*/
protected $path;
/**
* @AssertFile(maxSize="6000000")
*/
private $file;
private $temp;
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path;
}
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/art';
}
public function getFixturesPath()
{
return $this->getAbsolutePath() . 'web/uploads/art/fixtures/';
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* @ORMPrePersist()
* @ORMPreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique filename
$filename = sha1(uniqid(mt_rand(),true));
$this->path = $filename . '.' . $this->getFile()->guessExtension();
}
}
/**
* @ORMPostPersist()
* @ORMPostUpdate()
*/
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// if there is an error 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
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir() . '/' . $this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* @ORMPostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
夹具: <?php
// src/User/MyBundle/DataFixtures/ORM/ArtFixtures.php
namespace UserMyBundleDataFixturesORM;
use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonDataFixturesOrderedFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use FredzzLotwBundleEntityArt;
use SymfonyComponentHttpFoundationFileUploadedFile;
class ArtFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$art1 = new Art();
$art1->setTitle('MyTitle');
$art1->setDescription('My description');
copy($art1->getFixturesPath() . '01.jpg',$art1->getFixturesPath() . '01-copy.jpg');
$file = new UploadedFile($art1->getFixturesPath() . '01-copy.jpg',true);
$art1->setFile($file);
$art1->setUser($manager->merge($this->getReference('user-1')));
$manager->persist($art1);
$manager->flush();
}
}
希望这有助于某人!对不起,如果有什么问题.我还在学习 :) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
