dth42345 2014-07-07 14:51
浏览 60

在链配置的命名空间异常中找不到Symfony2多文件上载

So I've been working on getting my website to upload multiple files at once, and I've gotten pretty close (I think), but I'm getting the following error "The class 'Symfony\Component\HttpFoundation\File\UploadedFile' was not found in the chain configured namespaces". I think it's because I'm uploading the file as

$data = $form->getData();
$files = $data['file'];

foreach($files as $file)
{
    $photo = new Photo();
    $photo->setFile($file);
    $em->persist($file);
}

instead of connecting $photo to the createFormBuilder, if that makes any sense. I'm not sure what to do here, since I don't want to loose the auto_mapping from doctrine. Is there another workaround here, to make doctrine map UploadedFile? Here's my full code

Controller

Entity

Photo Entity YML

  • 写回答

2条回答 默认 最新

  • dongyuruan2957 2014-07-07 15:34
    关注

    You cannot do this. Doctrine tries to persist UploadedFile class and it is not entity class at all.

    Possible workarounds:

    • Save the filename
    • Save the file content (BLOB data)

    Generally, the easier way is to save file name only, BUT, you also need to move file, since the file is being uploaded to /tmp by default. Failing to move it will result in losing the file altogether.

    UploadedFile comes with useful method move which you could use to achieve file relocation.

    Another solution (less code but far more complex):

    About a week ago I released my own bundle that handles file uploads. You can find it here: https://github.com/jperovic/exploring-fileutility

    It tackles with this issue by moving the file and giving you the file name so you could write it to the database

    Disclaimer: it is not my intention to hijack you question with bundle promotion. Just say the word and I will remove it from answer.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体