Symfony 2: How do I manually assign a file to an entity which has been configured to use VichUploaderBundle to handle file uploads

StackOverflow https://stackoverflow.com/questions/22097541

Question

I have set up the VichUploaderBundle to facilitate the upload of a profile image for my User entity. I am using the GaufretteStorage option, configured to use the local filesystem and all is working fine when users upload an image via a form.

I now want to be able to assign a profile image file to a User entity in the code myself rather than relying on users to upload through the form. How do I do this?

I hope I am being clear enough.... essentially I have the contents of an image file (that I can grab through file_get_contents() or a similar measure) and I would like to assign this file to the User profile image attribute.

Was it helpful?

Solution

After contacting a maintainer of the bundle he provided this solution which answered my question:

Have a look at this https://github.com/dustin10/VichUploaderBundle/pull/200/files

Keep in mind that the bundle is triggered by entity lifecycle events (onPersist onLoad etc), but it will only manage the copy if an instance of UploadedFile is injected in the entity.

if you want to manually create the file but still manage it with the bundle you'll have to inject and UploadedFile in the entity.

OTHER TIPS

I don't know if i've understood you, but I think this can be helpful:

  1. Create a new attribute in your User entity "image" or "photo"
  2. Put the image in "web/uploads/user_1234.jpg", and store this path in "image" attribute created before.

If you need anything else, clarify your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top