Symfony VichUploaderBundle:无法生成文件名 [英] Symfony VichUploaderBundle: File name could not be generated

查看:95
本文介绍了Symfony VichUploaderBundle:无法生成文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VichUploader在symfony项目中上传文件.在配置中,我使用(从文档中复制):

I am using VichUploader to upload files within a symfony project. In configuration i use (copied from documentation):

service: vich_uploader.namer_property
options: { property: 'slug'}

在我的实体中,我会使用Gedmo/Sluggable自动生成弹头:

In my entity i generate the slugs automatically with Gedmo/Sluggable:

/**
 * @Gedmo\Slug(fields={"title"}, updatable=false)
 * @ORM\Column(type="string", length=100, nullable=false)
 */
protected $slug;

但是当尝试保存实体时,出现以下错误500:

But when trying to save the entity i get the following error 500:

无法生成文件名:属性slug为空.

File name could not be generated: property slug is empty.

如果我将属性设置为"title",则可以使用.我是否忘记了配置参数或其他使它与Gedmo slug配合使用的东西?

If i set the property to 'title' it works. Did i forget a configuration parameter or something else to get it working with the Gedmo slug?

推荐答案

此刻,我遇到了同样的问题,作为一种解决方法,我对实体类中的sl子吸气剂做了些改动:

I'm having the same issue at the moment, as a workaround, I've slightly changed the slug getter in the entity class:

use Gedmo\Sluggable\Util\Urlizer;

class Event
{
    // ...

    /**
     * @var string
     *
     * @Gedmo\Slug(fields={"name"})
     * @ORM\Column(name="slug", type="string", length=128, unique=true)
     */
    private $slug;

    // ...

    public function getSlug()
    {
        if (!$this->slug) {
            return Urlizer::urlize($this->getName());
        }

        return $this->slug;
    }
}

做到了.

不幸的是,有两个缺点:

Unfortunately, there're a couple of drawbacks:

  1. 如果您想在批注中更新可拖动行为以包括其他属性,则还必须更新getter.
  2. 此方法缺乏对数据库的检查:如果数据库中已经存在一个具有相同名称urlizer的记录,则无法在文件名中添加增量,则先前保存的文件可能会被覆盖!解决方法是,您可以将unique=true添加到易滞属性.
  1. If you ever want to update sluggable behaviour in the annotation to include additional properties, you'll have to update the getter as well.
  2. This method lacks a check against the database: if there's already a record in the database with the same name urlizer in the getter won't be able to add an increment to the file name, previously saved file may be overwritten! As a workaround, you can add unique=true to sluggable properties.

这篇关于Symfony VichUploaderBundle:无法生成文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆