-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cant upload Image #96
Comments
I've managed to make the upload working once but it seems I've changed something and it's not working anymore, failing silently. Could someone give us some hints so that we find the cause of that kind of problems ? Thanks |
Indeed! Never seen this before..like in with the Gedmo stuff it is enough to use it directly in the current method..why is it necessary to use it for the whole class? |
OK, I've just realized something odd: As I'm just actually trying this bundle to evaluate it, I have a very simple entity, TypeForm and I'm just using form_widget(form) to display all the fields of my form (4 field from my entity including the image field). If I do not change any other field than the image and press submit, nothing happens on server side and the form is sent back to my browser without any change (except the file browse field cleared). However, if I change any field + I browse for an image on my local FS to upload it, then it is uploaded correctly, written to the remote FS and updated in the DB. Why can't I just upload/update the image of my entity ? Is there something I do wrong ? |
@zabojad It's a known issue. The reason is, Doctrine2 manage the lifecycle of the entity and doctrine will check every property on your entity, about changes. If the entity has no chances, it will not fire the prePersist event. The current workaround is: <?php
$form->bind($request);
if ($form->isValid()) {
$em->persist($file);
$em->flush();
$this->container->get('vich_uploader.storage')->upload($entity);
return $this->redirect('/');
} |
Thanks ! I've put $this->container->get('vich_uploader.storage')->upload($entity); before the call of $em->persist() and $em->flush() of my entity and it works now. |
This workaround works, but this has no effect: delete_on_update: true I suppose it is because the hook is not really activated. Is there a way to handle this manually after $this->container->get('vich_uploader.storage')->upload($entity);? |
@diaspar It should work. Is there any error message? |
Sorry for not replying before. I think I have isolated what the problem is. I am using fos_user_bundle and this function to update a user: $userManager->updateUser($entity); I am experiencing the same errors as in issues/29 It appears VichUploader is trying to get twice the image from the tmp folder and at second time fails because it already moved it. |
As @Baachi said this is a known issue, but his proposed solution will lead to the problems you're getting now. The bundle uses doctrine lifecycle callbacks to trigger the file upload, with no changes in the entity (the uploaded file is not persisted so it doesn't count as a change) post persist is not triggered and nothing happens. If you manually call the upload method like:
You'll have a single upload when you change only the image (no other change in the entity), but you'll have a double upload when you change some other fields (the manual one and the one triggered by the callback). I think that is your problem. A better way to solve this is use and updateAt field on the entity so even if you're updating just the image, the entity change and the callback is triggered. Look at #8 (comment) for more detail |
@ftassi A option, to disable the pre_persist event listener would be nice. |
Definitely 👍 I think the bundle should not rely on callbacks to trigger the upload, doing it manually would be much better, it doesn't mix responsibilities and allows the user to have more control. Having an option that allows users to enable or disable the listener would be excellent, I've planned to do this, but I really didn't have time to do it. If you can work on it, go ahead. I'd keep the listener enabled by default by now, just to keep the old behavior for a while. |
@ftassi Will make a PR this week. |
ping |
Hi,
i am trying to get the Bundle working. I tried out a basic configuration but when i persist an entity to my database, the image is not uploaded and also there isnt a name saved in the table.
maybe someone of you have an idea:
config: https://gist.github.com/4502418
entity: https://gist.github.com/4502425
form: https://gist.github.com/4502433
template: https://gist.github.com/4502434
controller: https://gist.github.com/4502428
The text was updated successfully, but these errors were encountered: