3

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.

4

2 回答 2

2

在联系了捆绑包的维护者后,他提供了这个解决方案来回答我的问题:

看看这个https://github.com/dustin10/VichUploaderBundle/pull/200/files

请记住,捆绑包由实体生命周期事件(onPersist onLoad 等)触发,但只有在实体中注入 UploadedFile 的实例时,它才会管理副本。

如果您想手动创建文件但仍使用捆绑包管理它,则必须在实体中注入和 UploadedFile。

于 2014-03-03T13:43:16.227 回答
0

我不知道我是否理解你,但我认为这可能会有所帮助:

  1. 在您的用户实体“图像”或“照片”中创建一个新属性
  2. 将图片放在“web/uploads/user_1234.jpg”中,并将此路径存储在之前创建的“image”属性中。

如果您需要其他任何内容,请澄清您的问题。

于 2014-02-28T14:35:36.343 回答