Symfony 2.7使用VichUploaderBundle

时间:2015-11-26 16:36:54

标签: symfony twig vichuploaderbundle

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

  

在渲染模板期间抛出了异常   (“找不到字段的映射”ntec_image“”)in   第108行的ntec / ntec_edit.html.twig

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

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

Ther是我的代码:

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 %}

任何帮助将不胜感激!

罗杰

1 个答案:

答案 0 :(得分:4)

已解决!

只需输入&#34; imageFile&#34;像这样的树枝:

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