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

查看:114
本文介绍了如何将文件上传添加到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.

推荐答案

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

I've found the answer to my question. I need to use the class Symfony\Component\HttpFoundation\File\File 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天全站免登陆