3

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

在第 108 行的 ntec/ntec_edit.html.twig 中呈现模板(“未找到字段“ntec_image”的映射”)时引发异常

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

在简历中:我有一个类“tech_note”,这有一组图像“。

这是我的代码:

图片.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;
        }
    }

配置.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 %}

任何帮助将不胜感激!

罗杰

4

1 回答 1

4

解决了!

只需像这样将“imageFile”放在树枝中:

{% for image in ntec.images %} 
    <img src="{{ vich_uploader_asset(image, 'imageFile') }}" alt="{{ image.name }}" /> 
{% endfor %}
于 2015-12-02T14:28:43.730 回答