orn*_*rnj 15 php doctrine symfony symfony-2.1
我似乎无法理解如何将文件上传添加到DataFixture.我正在尝试为我的灯具加载的虚拟内容上传图像.这似乎是有用的知识.
小智 19
虽然这个问题在1年前被问过,但似乎没有很多关于如何通过学说数据夹具上传文件的信息.我只能找到这篇文章.
我一直在寻找,而且我采取的方法与ornj略有不同.(可能与Symfony的更新有关.)
我首先要这样做
use Symfony\Component\HttpFoundation\File\UploadedFile;
Run Code Online (Sandbox Code Playgroud)
然后使用copy()复制图像,因为ornj表示将移动它.
copy($art1->getFixturesPath() . '01.jpg', $art1->getFixturesPath() . '01-copy.jpg');
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令创建和添加文件:
$file = new UploadedFile($art1->getFixturesPath() . '01-copy.jpg', 'Image1', null, null, null, true);
$art1->setFile($file);
$manager->persist($art1);
Run Code Online (Sandbox Code Playgroud)
如果我没有在''UploadedFile''构造函数中将最后一个参数设置为''true'',因为它在运行''doctrine:fixtures:load''时会抛出一个未知错误.此参数为"测试模式是否处于活动状态".看到它是一个夹具,设置为测试模式是有意义的.
方法''getFixturesPath()''只检索存储样本图像的路径:
// Entity file
public function getFixturesPath()
{
return $this->getAbsolutePath() . 'web/uploads/art/fixtures/';
}
Run Code Online (Sandbox Code Playgroud)
'getAbsolutePath()''方法取自Doctrine File Uploads.
完整的工作代码:实体:
<?php
//src/User/MyBundle/Entity/Art.php
namespace User/MyBundle/Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
*
* Art Entity
*
* @ORM\Entity(repositoryClass="User\MyBundle\Entity\Repository\ArtRepository")
* @ORM\Table(name="art")
* @ORM\HasLifecycleCallbacks
*/
class Art
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $path;
/**
* @Assert\File(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;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
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();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
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;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
Run Code Online (Sandbox Code Playgroud)
夹具:
<?php
// src/User/MyBundle/DataFixtures/ORM/ArtFixtures.php
namespace User\MyBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Fredzz\LotwBundle\Entity\Art;
use Symfony\Component\HttpFoundation\File\UploadedFile;
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', 'Image1', null, null, null, true);
$art1->setFile($file);
$art1->setUser($manager->merge($this->getReference('user-1')));
$manager->persist($art1);
$manager->flush();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人!对不起,如果有问题.我还在学习 :)
我找到了问题的答案.我需要使用该类Symfony\Component\HttpFoundation\File\File来创建一个文件.Symfony将物理移动文件而不创建副本,因此您需要为每个fixture添加一个新文件,使用use copy()来创建可以移动的文件的副本.
$image = new Image();
$file = new File('path/to/file.jpg');
$image->file = $file;
$om->persist($image);
Run Code Online (Sandbox Code Playgroud)
这样的事情.
| 归档时间: |
|
| 查看次数: |
7726 次 |
| 最近记录: |