在 symfony 3 中使用 VichUploaderBundle 显示树枝图像时出现问题

problem by displaying a twig image with VichUploaderBundle in symfony 3

我使用 vichupload 上传我的图像,它 works.Now 当我想使用记录的图像时,我只有个人资料中显示的图像名称,而没有照片。 我使用实体 Media 和 Medecin 之间的关系 OneToOne 这是实体媒体:

  <?php

namespace Doctix\MedecinBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
 * Media
 *
 * @ORM\Table(name="media")
 * 

 @Vich\Uploadable
*/

class Media 
{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

// ... other fields

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

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string
 */
private $imageName;

/**
 * @ORM\Column(type="integer")
 *
 * @var integer
 */
private $imageSize;

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

public function getId()
{
    return $this->id;
}

/**
 * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
 * of 'UploadedFile' is injected into this setter to trigger the  update. If this
 * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
 * must be able to accept an instance of 'File' as the bundle will inject one here
 * during Doctrine hydration.
 *
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 */
public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if (null !== $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 \DateTimeImmutable();
    }
}

public function getImageFile()
{
    return $this->imageFile;
}

public function setImageName(string $imageName)
{
    $this->imageName = $imageName;
}

public function getImageName()
{
    return $this->imageName;
}

public function setImageSize(int $imageSize)
 {
    $this->imageSize = $imageSize;
 }

 public function getImageSize(): int
{
    return $this->imageSize;
}

 }

这是 Media 实体与 Medecin 实体具有 OneToOne 关系的部分:

 /**
 * @ORM\OneToOne(targetEntity="Doctix\MedecinBundle\Entity\Media", cascade={"persist","remove","refresh"})
 * @ORM\JoinColumn(nullable=true)
 */
private $media;

以及我调用图像的视图:

 <figure>
                            <a href="#"><img src="{{ vich_uploader_asset(medecin.media, 'imageFile') }}" alt="{{ medecin.media.imagename }}">   </a>
                                </figure>

这是我的 vich_uploader 配置:

 vich_uploader:
db_driver: orm 


mappings:
    media_image:
        uri_prefix: /images/medias 
        upload_destination: '%%kernel.project_dir%/public/images/medias'

        inject_on_load: false
        delete_on_update: true
        delete_on_remove: true

谢谢

您在 upload_destination: '%%kernel.project_dir%/public/images/medias' 处有 2 个百分号,因此上传到错误的位置。

(或者根本没有上传。)