The file upload is not picked up, if the file is the only field that has changed #123

Closed
WishCow opened this Issue Mar 5, 2013 · 13 comments

Comments

Projects
None yet
9 participants
@WishCow

WishCow commented Mar 5, 2013

If you have a user model, that has 4 attributes:

  • id
  • name
  • picture
  • picturePath

Id, name, and picturePath are persisted through Doctrine, the picture attribute is the one with the @Vich/UploadableField annotation, and has "picturePath" as its fileNameProperty attribute.

Create a form for this entity, then upload a new file, and modify nothing else on the form. The change will not be picked up. This is because the UploaderListener is listening on the prePersist and preUpdate events, which are never fired, since the only field that is modified by the form is the "picture" field, which is not mapped by Doctrine.

@sheeep

This comment has been minimized.

Show comment
Hide comment
@sheeep

sheeep Mar 6, 2013

This is because the UploaderListener is listening on the prePersist and preUpdate events, which are never fired, since the only field that is modified by the form is the "picture" field, which is not mapped by Doctrine.

I had the exact same problem today and solved it with a pretty nasty workaround. I have a field updated which is mapped by Doctrine (and handled by Gedmo\Timestampable) so I just update this field every time I update the base entity.

$image->setUpdated(new \DateTime());

This way it will always update your image field. Indeed this is not the solution I was looking for, but a pretty painless one after all.

sheeep commented Mar 6, 2013

This is because the UploaderListener is listening on the prePersist and preUpdate events, which are never fired, since the only field that is modified by the form is the "picture" field, which is not mapped by Doctrine.

I had the exact same problem today and solved it with a pretty nasty workaround. I have a field updated which is mapped by Doctrine (and handled by Gedmo\Timestampable) so I just update this field every time I update the base entity.

$image->setUpdated(new \DateTime());

This way it will always update your image field. Indeed this is not the solution I was looking for, but a pretty painless one after all.

@WishCow

This comment has been minimized.

Show comment
Hide comment
@WishCow

WishCow Mar 7, 2013

I ended up with something similar, I modified the setPictureFile() method on my entity:

public function setPictureFile($pictureFile) {
    $this->pictureFile = $pictureFile;
    if ($pictureFile) {
        $this->picture = $pictureFile->getFileName();
    }
}

The pictureFile attribute is the one with the uploader annotation. This seems somewhat safe, since it will be overwritten at flush time by the uploader.

WishCow commented Mar 7, 2013

I ended up with something similar, I modified the setPictureFile() method on my entity:

public function setPictureFile($pictureFile) {
    $this->pictureFile = $pictureFile;
    if ($pictureFile) {
        $this->picture = $pictureFile->getFileName();
    }
}

The pictureFile attribute is the one with the uploader annotation. This seems somewhat safe, since it will be overwritten at flush time by the uploader.

@Baachi

This comment has been minimized.

Show comment
Hide comment
@Baachi

Baachi Mar 7, 2013

Contributor

@WishCow @sheeep this is related to the issue #8 and PR #106.

Contributor

Baachi commented Mar 7, 2013

@WishCow @sheeep this is related to the issue #8 and PR #106.

@boonkerz boonkerz referenced this issue in symfony2admingenerator/AdmingeneratorGeneratorBundle Apr 4, 2013

Closed

Upload Field #396

@gido

This comment has been minimized.

Show comment
Hide comment
@gido

gido Nov 18, 2013

Contributor

lost some time today to understand why the file upload don't work anymore. We should add something about this "issue" in the doc.

Contributor

gido commented Nov 18, 2013

lost some time today to understand why the file upload don't work anymore. We should add something about this "issue" in the doc.

@ftassi

This comment has been minimized.

Show comment
Hide comment
@ftassi

ftassi Nov 18, 2013

Collaborator

Duplicated by #8

Collaborator

ftassi commented Nov 18, 2013

Duplicated by #8

@ftassi ftassi closed this Nov 18, 2013

ftassi added a commit that referenced this issue Nov 18, 2013

ftassi added a commit that referenced this issue Nov 18, 2013

@hediyedel

This comment has been minimized.

Show comment
Hide comment
@hediyedel

hediyedel Nov 19, 2013

Hi everyone. I'm new at symfony.
I installed VichUploaderBundle ,but I didn't understand how to use it in my project.
I wonder if you could help me please?

Hi everyone. I'm new at symfony.
I installed VichUploaderBundle ,but I didn't understand how to use it in my project.
I wonder if you could help me please?

@ftassi

This comment has been minimized.

Show comment
Hide comment
@ftassi

ftassi Nov 19, 2013

Collaborator

@hediyedel You should read the doc and then come back with a more specific question. If something is not clear in the doc let us know.

Collaborator

ftassi commented Nov 19, 2013

@hediyedel You should read the doc and then come back with a more specific question. If something is not clear in the doc let us know.

@hediyedel

This comment has been minimized.

Show comment
Hide comment
@hediyedel

hediyedel Nov 19, 2013

@ftassi I've read it and done all changes, but when I copy {{ product.name }} in my twig or
$entity = // get the entity..
$helper = $this->container->get('vich_uploader.templating.helper.uploader_helper');
$path = $helper->asset($entity, 'image');
in my controller,I have this error: " The object is not uploadable. 500 Internal Server Error - InvalidArgumentException "
How can I solve it?

@ftassi I've read it and done all changes, but when I copy {{ product.name }} in my twig or
$entity = // get the entity..
$helper = $this->container->get('vich_uploader.templating.helper.uploader_helper');
$path = $helper->asset($entity, 'image');
in my controller,I have this error: " The object is not uploadable. 500 Internal Server Error - InvalidArgumentException "
How can I solve it?

@ftassi

This comment has been minimized.

Show comment
Hide comment
@ftassi

ftassi Nov 19, 2013

Collaborator

Double check you annotation on the Entity class and open a dedicated issue for your problem, this one is not related to it.

Collaborator

ftassi commented Nov 19, 2013

Double check you annotation on the Entity class and open a dedicated issue for your problem, this one is not related to it.

@AlmogBaku

This comment has been minimized.

Show comment
Hide comment
@AlmogBaku

AlmogBaku Apr 5, 2014

is there no any solution yet?

is there no any solution yet?

@DavidM974

This comment has been minimized.

Show comment
Hide comment
@DavidM974

DavidM974 Feb 1, 2016

Hi, i'm testing your bundle and when i save my Image entity the file is lost. My conf is like the exemple. Is it possible that my event listener is not call? even if my updatedAt is update in my database?

Hi, i'm testing your bundle and when i save my Image entity the file is lost. My conf is like the exemple. Is it possible that my event listener is not call? even if my updatedAt is update in my database?

@K-Phoen

This comment has been minimized.

Show comment
Hide comment
@K-Phoen

K-Phoen Feb 1, 2016

Collaborator

@DavidM974 Can you open a new issue for your problem? Giving us your configuration would be nice too :)

Collaborator

K-Phoen commented Feb 1, 2016

@DavidM974 Can you open a new issue for your problem? Giving us your configuration would be nice too :)

@DavidM974

This comment has been minimized.

Show comment
Hide comment
@DavidM974

DavidM974 Feb 1, 2016

Thanks for your quick answer my bad!
i forgot the annotation on the class ...
* @Vich\Uploadable */ class Image {

Now it works!

Thanks for your quick answer my bad!
i forgot the annotation on the class ...
* @Vich\Uploadable */ class Image {

Now it works!

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 2, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 2, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 3, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment