简体   繁体   中英

Images not being saved when they are the only field changed

I filed an issue ticket with symfony on github, but I need a workaround: https://github.com/symfony/symfony/issues/5150

I created an entity with an associated image, and I'm saving it using a lifecycle callback as described in the symfony docs: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-lifecycle-callbacks

When A user filled out the form, and hits save, everything works fine. However, if the only item the user fills/edits is the image, then the form is not saved. I believe this is because symfony doesn't see a change to the entity, so it doesn't bother to repersist the entity, and thus the pre/post persist functions are never called.

I need some way to force the entity to persist.

You can add a updated Date field. At the setter of your file line set this updated field to the current date. Now everything should work fine.

I assume you have a field for file uploads in your form and a field for the path. If you change your setFile method to change the file path entity it should save correctly. For example:

public function setFile($file)
{
    if (! empty($file)) {
        $this->photo = 'changed';
    }
    $this->file = $file;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM