我对 VichUploaderBundle 有问题,我按照所有说明进行操作,我可以让它上传图像,现在我不知道如何使用 twig 渲染图像。我有一个 Product 实体,它与 Photo 具有 OneToOne 关系(我使用带有 VichUploaderBundle 的实体照片)。
我的 html.twig 文件
{% for product in products %}
<img src="{{ vich_uploader_asset(product, 'image') }}" alt="{{ product.nombre }}" />
{% endfor %}
这给了我以下错误,
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.
所以我在 img 标签中添加了以下内容
<img src="{{ vich_uploader_asset(product, 'image','AppBundle\Entity\Product') }}" alt="{{ product.nombre }}" />
并向我抛出这个错误
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.
我的实体存储在 AppBundle\Entity\ * 中,它还删除了斜线。
我也尝试添加它但没有成功。
<img src="{{ vich_uploader_asset(product.photo, 'image','AppBundle\Entity\Product') }}" alt="{{ product.nombre }}" />
我的照片实体(这是捆绑说明中的复制和粘贴)
<?php
//src/AppBundle/Entity/Photo.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;
/**
* @ORM\Entity
* @ORM\Table(name="photo")
* @Vich\Uploadable
*/
/*
This class represents the images
*/
class Photo
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName")
*
* @var File $imageFile
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, name="image_name")
*
* @var string $imageName
*/
protected $imageName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime $updatedAt
*/
protected $updatedAt;
/**
* 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 ($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;
}
/**
* @param string $imageName
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
}
这是我的产品实体
<?php
//src/AppBundle/Entity/Product.php
/*
This class represents a product, for instance, we'll be using
only product.
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="product")
* ORM\Entity(repositoryClass="AppBundle\Repositories\ProductRepository")
*/
//extends objComercio has been removed for simplicity.
class Product
{
public function __construct()
{
$this->photos = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string",length=255, nullable=false)
*/
protected $nombre;
/**
* @ORM\Column(type="string",length=255, nullable=false)
*/
protected $descripcion;
/**
* @ORM\Column(type="integer",nullable=false)
*/
protected $precio;
/**
* @ORM\Column(type="string",length=255, nullable=false)
*/
protected $empresa;
/**
* @ORM\OneToOne(targetEntity="Photo", cascade={"persist"})
*/
protected $photo;
/**
* @ORM\ManyToOne(targetEntity="Store",inversedBy="products")
*/
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;
}
/**
* Set store_product
*
* @param \AppBundle\Entity\Store $storeProduct
* @return Product
*/
public function setStoreProduct(\AppBundle\Entity\Store $storeProduct = null)
{
$this->store_product = $storeProduct;
return $this;
}
/**
* Get store_product
*
* @return \AppBundle\Entity\Store
*/
public function getStoreProduct()
{
return $this->store_product;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* @param string $nombre
* @return Product
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* @return string
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Set descripcion
*
* @param string $descripcion
* @return Product
*/
public function setDescripcion($descripcion)
{
$this->descripcion = $descripcion;
return $this;
}
/**
* Get descripcion
*
* @return string
*/
public function getDescripcion()
{
return $this->descripcion;
}
}
我使用 {{}} 打印了所有内容,一切正常。
我不知道还能做什么。
提前致谢!