VichUploaderBundle使用树枝渲染图像的问题


Problems with VichUploaderBundle render image using twig

我对VichUploaderBundle有问题,我遵循了所有的说明,我可以让它上传一个图像,现在我不知道如何使用trick渲染图像。我有一个产品实体,它与照片有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;
}
}

我使用{{}}打印了所有内容,一切都很好。

我不知道还能做什么。

提前感谢!

我知道这真的很老了,但对谷歌来说仍然很重要。我认为这应该有效:

  • 放置"imageFile"而不是"image"
  • 将className的斜杠加倍;

    {{vich_uloader_asset(product,'imageFile','AppBundle''''Entity''''product')}}

这是一个老问题,但这让我和刚接触Symfony、Doctrine和VichUploaderBundle的人都感到困惑,这个问题在谷歌中最相关。如果你把这一切都弄错了,一个常见的Twig错误是Mapping not found for field,我把它包括在内是因为谷歌的原因!

Doctrine返回一组Product对象,其中一个Photo对象作为每个Product的子对象。vich_uploloader_asset需要渲染图像的就是这个Photo子对象。因此,使用点访问每个产品对象的照片子对象:product.photo,而不仅仅是product

vich_uploloader_asset的第二个参数是添加UploadableField注释/配置的Photo实体中使用的字段名。在上面的示例中,这是imageFile,而不是image。在vich_uploloader_asset文档中,image是字段名,所以这可能是您刚刚复制代码的地方。

vich_uploloader_asset的第三个参数是指定可上传实体的类名,同样,它不是Product,而是Photo。但实际上您并不需要它,因为只要您指定product.photo,vich就会找到它。但如果你确实需要它,你需要包括两个反斜杠,正如@StefanNch所指出的。

这是你需要的线路:

<img src="{{ vich_uploader_asset(product.photo, 'imageFile') }}" alt="{{ product.nombre }}" />

我认为你必须根据你的照片实体配置来显示你的照片:

<img src="{{ vich_uploader_asset(product.photo, 'image') }}" alt="{{ product.nombre }}" />

<img src="{{ vich_uploader_asset(product.photo, 'file') }}" alt="{{ product.nombre }}" />

我终于设法让它工作起来了。我已经写了一些angularjs代码(我也在我的网站上使用它)。以下是我所做的

我的main.js

 $http.get('/get-products-by-user').success(function(data){
    $scope.products = data;
})

路由.yml

get_products_by_user:
  path: /get-products-by-user
  defaults: { _controller: AppBundle:Product:getAllProductsByUser}

productController:getAllProductsByUser

 public function getAllProductsByUserAction()
{
    $products = $this->getDoctrine()
        ->getRepository('AppBundle:Product')
        ->findAllProductsByUser($this->getUser()->getId());
    return new Response(json_encode($products),200);
}

我的list-all-products.html.twig行,我在其中渲染图像

<div ng-repeat="product in products | filter: query" >
   <img ng-src="/images/products/{[{product.photo.imageName }]}" class="img-responsive img-thumbnail size-img" alt="{[{ product.nombre }]}"/></div>

我的配置yml,vich上传部分

 mappings:
    product_image:
        uri_prefix: /images/products
        upload_destination: %kernel.root_dir%/../web/images/products

我不知道这是否是一个使用捆绑包的好方法(它只是起作用),我只是使用vichUploader上传图像,然后我做了一个查询,以获取我的所有产品,包括文件名,一旦我得到它,我就使用uri_prefix/{〔{product.imageName}〕}来渲染图像。

({〔{}〕},我同时使用angularjs和trick模板引擎,这就是我使用该语法的原因)

在日期是对象的情况下尝试此操作

<img src="{{ path ~ vich_uploader_asset(data, 'imageFile', 'App''Entity''Crente') }}" style="margin-top: -20px" />