简体   繁体   English

当图像是唯一更改的字段时,图像不会被保存

[英]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 我在github上用symfony提交了一张发行票,但我需要一个解决方法: 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 我创建了一个带有关联图像的实体,我正在使用symfony文档中描述的生命周期回调来保存它: http//symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-lifecycle-回调

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. 我相信这是因为symfony没有看到实体的变化,因此它不会重复执行实体,因此永远不会调用前/后持久化函数。

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. 在文件行的setter中,将此更新字段设置为当前日期。 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. 如果更改setFile方法以更改文件路径实体,则应正确保存。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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