Symfony 2.7 使用 VichUploaderBundle 在树枝中渲染图像

Symfony 2.7 Render image in twig with VichUploaderBundle

我有一个带有 VichUplaoderBundle 的 Symfony 2.7,当我尝试渲染图像时出现此错误:

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

保存、编辑和删除图像的操作正常

在简历中:我有一个 class "tech_note" 并且这是一组图像。

这是我的代码:

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

树枝

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

已解决

只需要将"ntec_image"改为"imageFile"....

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

任何帮助将不胜感激!

罗杰

已解决!

只需将 "imageFile" 放在树枝中,如下所示:

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