如何在 yml 映射的实体中配置 VichUploader?

2022-01-16 00:00:00 php symfony doctrine vichuploaderbundle

我有一个名为杂志"的实体,从 yml 文件映射:

I have a entity called 'Magazine', mapped from yml file:

AcmeDemoBundleEntityMagazine:
  type: entity
  table: magazine
  id:
    id:
      type: integer
      generator: { strategy: AUTO }

  fields:
    edition:
        type: string
        length: 255
        nullable: false
    title:
        type: text
        nullable: false
    cover:
        type: string
        length: 255
        nullable: false
    file:
        mapping: magazine_cover
        filename_property: cover

我在 app/config/config.yml 中做了必要的配置:

I made the configuration necessary in the app/config/config.yml:

knp_gaufrette:
  stream_wrapper: ~

vich_uploader:
  db_driver: orm
  mappings:
    magazine_cover:
      uri_prefix:         /upload/magazine/cover
      upload_destination: %kernel.root_dir%/../web/upload/magazine/cover
      delete_on_remove:   true

实体文件:

use DoctrineORMMapping as ORM;

use SymfonyComponentHttpFoundationFileFile;
use SymfonyComponentValidatorConstraints as Assert;
use VichUploaderBundleMappingPropertyMapping as Vich;

我没有以这种方式工作,在杂志"表中创建了一个字段文件",这不应该发生.我发现一些文章解释了如何使用注释进行配置,但 yml 我什么也没找到.

I'm not getting work this way, a field 'file' is created in the 'magazine' table, which doesnt should happens. I've found some articles explaining how configure using annotation but yml I don't found nothing.

推荐答案

如果您阅读 文档仔细,你会注意到上传相关的配置并没有与学说的实体声明混合.

If you read the documentation carefully, you'll notice that the upload-related configuration isn't mixed with doctrine's entity declaration.

您需要创建一个 src/Acme/DemoBundle/Resource/config/vich_uploader/Magazine.yml 文件,内容如下:

You need to create a src/Acme/DemoBundle/Resource/config/vich_uploader/Magazine.yml file with the following content:

AcmeDemoBundleEntityMagazine:
    file:
        mapping: magazine_cover
        filename_property: cover

您将在我的 沙盒应用程序中找到工作代码示例.

相关文章