cbacelar
cbacelar

Reputation: 553

How to configure VichUploader in entity mapped by yml?

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

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

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

Entity file:

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;

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.

Upvotes: 3

Views: 2907

Answers (1)

K-Phoen
K-Phoen

Reputation: 1270

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

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

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

You will find working code samples in my sandbox application.

Upvotes: 2

Related Questions