Skip to content
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

Closed
soerenmartius opened this issue Jan 10, 2013 · 14 comments
Closed

Cant upload Image #96

soerenmartius opened this issue Jan 10, 2013 · 14 comments
Labels

Comments

@soerenmartius
Copy link

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

@zabojad
Copy link

zabojad commented Jan 10, 2013

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

@zabojad
Copy link

zabojad commented Jan 10, 2013

Your entity should be annotate with @vich\Uploadable:

/**

  • Message
    *
  • @Orm\Table(name="ab_message")
  • @Orm\Entity
  • @vich\Uploadable
    */
    class Message

@soerenmartius
Copy link
Author

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?

@zabojad
Copy link

zabojad commented Jan 10, 2013

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 ?

This was referenced Jan 10, 2013
@Baachi
Copy link
Contributor

Baachi commented Jan 11, 2013

@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('/');
}

@zabojad
Copy link

zabojad commented Jan 11, 2013

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.

@diaspar
Copy link

diaspar commented Jan 13, 2013

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);?

@Baachi
Copy link
Contributor

Baachi commented Jan 13, 2013

@diaspar It should work.
This workaround does the same as the entity listener.

Is there any error message?

@diaspar
Copy link

diaspar commented Jan 16, 2013

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);
$this->container->get('vich_uploader.storage')->upload($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.

@ftassi
Copy link
Collaborator

ftassi commented Jan 16, 2013

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:

I am using fos_user_bundle and this function to update a user:
$userManager->updateUser($entity);
$this->container->get('vich_uploader.storage')->upload($entity);

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

@Baachi
Copy link
Contributor

Baachi commented Jan 16, 2013

@ftassi A option, to disable the pre_persist event listener would be nice.
Would you accept a PR?

@ftassi
Copy link
Collaborator

ftassi commented Jan 16, 2013

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.
Don't forget to update the doc about the new option.

@Baachi
Copy link
Contributor

Baachi commented Jan 16, 2013

@ftassi Will make a PR this week.

@nifr
Copy link

nifr commented Apr 22, 2013

ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants