Search code examples
symfonyvichuploaderbundle

problem by displaying a twig image with VichUploaderBundle in symfony 3


I use vichupload to upload my images and it works.Now when I want to use the recorded images, I only have the name of the image that appears in the profile and not the photo. I use the entity Media and Medecin with a relation OneToOne between Here is the entity Media:

  <?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;
}

 }

here is the part where the Media entity has a OneToOne relationship with the Medecin entity:

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

And my view where I call on the image:

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

Here is my config of 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

Thanks


Solution

  • You have 2 percent signs at upload_destination: '%%kernel.project_dir%/public/images/medias' so it is uploaded to a wrong place.

    (Or not uploaded at all.)