Search code examples
symfonytwigvichuploaderbundle

Symfony 2.7 Render image in twig with VichUploaderBundle


I have a Symfony 2.7 with VichUplaoderBundle and when I try to render a image I have this error:

An exception has been thrown during the rendering of a template ("Mapping not found for field "ntec_image"") in ntec/ntec_edit.html.twig at line 108

The action to save, edit and remove images it's working correctly

In resume: I have one class "tech_note" and this has a collection of images".

Ther is my code:

Image.php

<?php

    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\HttpFoundation\File\File;
    use Vich\UploaderBundle\Mapping\Annotation as Vich;

    /**
     * AppBundle\Entity\Image
     *
     * @ORM\Table(name="image")
     * @ORM\Entity
     * @ORM\Entity(repositoryClass="AppBundle\Entity\ImageRepository")
     * @Vich\Uploadable
     */
    class Image {

        /*more awesome code here*/

        /**
         * NOTE: This is not a mapped field of entity metadata, just a simple property.
         * 
         * @Vich\UploadableField(mapping="ntec_image", fileNameProperty="name")
         * 
         * @var File
         */
        private $imageFile;

        /**
         * @ORM\Column(type="datetime", nullable=true))
         *
         * @var \DateTime
         */
        private $updatedAt;

        /**
         * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
         */
        public function setImageFile(File $image = null) {
            $this->imageFile = $image;

            if ($image) {
                // It is required that at least one field changes if you are using doctrine
                // otherwise the event listeners won't be called and the file is lost
                $this->updatedAt = new \DateTime('now');
            }
        }

        /**
         * @return File
         */
        public function getImageFile() {
            return $this->imageFile;
        }
    }

config.yml

vich_uploader:
    db_driver: orm
    mappings:
        ntec_image:
            uri_prefix:         /images/ntec
            upload_destination: %kernel.root_dir%/../web/images/ntec
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
            namer:              vich_uploader.namer_uniqid

twig

{% for image in ntec.images %}
     <img src="{{ vich_uploader_asset(image, 'ntec_image') }}" alt="{{ image.name }}" />
{% endfor %}

SOLVED

Only need change "ntec_image" to "imageFile"....

{% for image in ntec.images %}
     <img src="{{ vich_uploader_asset(image, 'imageFile') }}" alt="{{ image.name }}" />
{% endfor %}

Any help will be appreciated!

Roger


Solution

  • SOLVED!

    just put "imageFile" in twig like this:

    {% for image in ntec.images %} 
        <img src="{{ vich_uploader_asset(image, 'imageFile') }}" alt="{{ image.name }}" /> 
    {% endfor %}