- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我似乎无法理解如何将文件上传添加到 DataFixture。我正在尝试为我的固定装置加载的虚拟内容上传图像。了解这些似乎很有用。
最佳答案
虽然这个问题是在 1 年前提出的,但似乎没有太多关于如何通过条令数据夹具上传文件的信息。我只能找到这篇文章。
我一直在寻找,我采用了与 ornj 的方法略有不同的方法。 (可能与 Symfony 的更新有关。)
我首先必须
use Symfony\Component\HttpFoundation\File\UploadedFile;
然后使用 copy() 复制图像,因为正如 ornj 所说,它会移动它。
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);
$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 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);
}
}
}
夹具:
<?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();
}
}
希望这对某人有帮助!抱歉,如果有什么问题。我还在学习:)
关于php - 您将如何添加文件上传到 Symfony2 DataFixture?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12309077/
如何访问 EntityManager 或 DataFixture 中的存储库类以创建查询? 最佳答案 如果您的夹具实现 ContainerAwareInterface您可以完全访问容器,并且可以从那里
我似乎无法理解如何将文件上传添加到 DataFixture。我正在尝试为我的固定装置加载的虚拟内容上传图像。了解这些似乎很有用。 最佳答案 虽然这个问题是在 1 年前提出的,但似乎没有太多关于如何通过
我正在尝试使用一些用户对象预先填充数据库,但是当我调用 $user->setPassword('some-password'); 然后保存用户对象时,字符串 'some-password' 直接存储在
我正在尝试使用一些用户对象预先填充数据库,但是当我调用 $user->setPassword('some-password'); 然后保存用户对象时,字符串 'some-password' 直接存储在
我在我的 User 数据装置中使用 LDAP,我不想硬编码 LDAP 登录选项。最初,我试过这个: $options = array( 'host' => '%ldap_hos
我有以下 phpUnit 功能测试: namespace Tests\AppBundle\Controller; /** * @testtype Functional */ class Default
我正在学习测试我的 Symfony2 代码,但我正在尝试构建测试,所以我正在使用 DataFixtures 加载数据,顺便说一句,我认为这也测试了我的实体。我将使用一个测试作为示例:CreateCom
我一直在根据食谱使用我的附件实体 How To Handle File Uploads With Doctrine在 Symfony 2.3 中。 即使在功能测试中,它也运行良好。然而,将它与 Doc
您好,出于学习目的,我正在使用 flex 与 Symfony 一起工作。在我安装了一些 recipes 之后,我想添加 nelmio/alice 来为 doctrine fixtures 生成假数据,
我是一名优秀的程序员,十分优秀!