在链配置的命名空间异常中找不到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

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.

Apparently I'm just an idiot. Spent hours trying to figure it out, only to find out that I was doing

$em->persist($file);

instead of

$em->persist($photo);

My bad