I have a problem with VichUploaderBundle, I followed all the instructions, and I could force the image to load, now I don’t know how to render images using the branch. I have a Product object that has OneToOne related to Photo (I use a photo of the object with VichUploaderBundle).
My html.twig file
{% for product in products %}
<img src="{{ vich_uploader_asset(product, 'image') }}" alt="{{ product.nombre }}" />
{% endfor %}
This gives me the following error:
An exception has been thrown during the rendering of a template ("Impossible to determine the class name. Either specify it explicitly or give an object") in products/list_all_products.html.twig at line 9.
So, I added the following to the img tag
<img src="{{ vich_uploader_asset(product, 'image','AppBundle\Entity\Product') }}" alt="{{ product.nombre }}" />
and gives me this error
An exception has been thrown during the rendering of a template ("Class AppBundleEntityProduct does not exist") in products/list_all_products.html.twig at line 9.
My objects are stored in AppBundle \ Entity \ *, and also remove slashes.
I also tried to add this without success.
<img src="{{ vich_uploader_asset(product.photo, 'image','AppBundle\Entity\Product') }}" alt="{{ product.nombre }}" />
My Photo object (this is a copy and paste from the package instructions)
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collection\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
class Photo
{
protected $id;
protected $imageFile;
protected $imageName;
protected $updatedAt;
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
$this->updatedAt = new \DateTime('now');
}
}
public function getImageFile()
{
return $this->imageFile;
}
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
public function getImageName()
{
return $this->imageName;
}
}
And here is my Product object
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
class Product
{
public function __construct()
{
$this->photos = new ArrayCollection();
}
protected $id;
protected $nombre;
protected $descripcion;
protected $precio;
protected $empresa;
protected $photo;
protected $store_product;
public function getPhoto()
{
return $this->photo;
}
public function setPhoto(\AppBundle\Entity\Photo $photo)
{
$this->photo = $photo;
}
public function getPrecio()
{
return $this->precio;
}
public function setPrecio($aPrice)
{
$this->precio = $aPrice;
}
public function getEmpresa()
{
return $this->empresa;
}
public function setEmpresa($anEnterprise)
{
$this->empresa = $anEnterprise;
}
public function setStoreProduct(\AppBundle\Entity\Store $storeProduct = null)
{
$this->store_product = $storeProduct;
return $this;
}
public function getStoreProduct()
{
return $this->store_product;
}
public function getId()
{
return $this->id;
}
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
public function getNombre()
{
return $this->nombre;
}
public function setDescripcion($descripcion)
{
$this->descripcion = $descripcion;
return $this;
}
public function getDescripcion()
{
return $this->descripcion;
}
}
I printed everything using {{}} and it all works great.
I am not going to do anything else.
Thanks in advance!