您如何将文件上传添加到 Symfony2 DataFixture? [英] How would you add a file upload to a Symfony2 DataFixture?

查看:26
本文介绍了您如何将文件上传添加到 Symfony2 DataFixture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法理解如何将文件上传添加到 DataFixture.我正在尝试为我的装置加载的虚拟内容上传图像.这似乎是有用的东西.

I can't seem to wrap my head around how I would go about adding a file upload to a DataFixture. I'm trying to upload an image for the dummy content my fixtures load. This seems like something that would be useful to know.

推荐答案

我找到了问题的答案.我需要使用 SymfonyComponentHttpFoundationFileFile 类来创建一个文件.Symfony 将物理移动文件而不是创建副本,因此您需要为每个夹具创建一个新文件,使用 copy() 创建可以移动的文件副本.

I've found the answer to my question. I need to use the class SymfonyComponentHttpFoundationFileFile to create a File. Symfony will physically move the file and not create a copy so you need to either have a new file for each fixture, use use copy() to create a copy of the file that can be moved instead.

$image = new Image();
$file = new File('path/to/file.jpg');
$image->file = $file;
$om->persist($image);

类似的东西.

这篇关于您如何将文件上传添加到 Symfony2 DataFixture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆