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

时间:2014-07-09 16:19:03

标签: php symfony doctrine vichuploaderbundle

我有一个名为' Magazine'的实体,从yml文件映射:

Acme\DemoBundle\Entity\Magazine:
  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中进行了必要的配置:

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 Doctrine\ORM\Mapping as ORM;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\PropertyMapping as Vich;

我没有以这种方式工作,一个字段'文件'是在杂志中创建的#39;表,不应该发生。我发现了一些文章解释了如何使用注释进行配置,但是我没有发现任何内容。

1 个答案:

答案 0 :(得分:2)

如果您仔细阅读the documentation,则会注意到与上传相关的配置并未与学说的实体声明混合。

您需要创建一个包含以下内容的src/Acme/DemoBundle/Resource/config/vich_uploader/Magazine.yml文件:

Acme\DemoBundle\Entity\Magazine:
    file:
        mapping: magazine_cover
        filename_property: cover

您可以在我的sandbox application找到工作代码示例。