【问题标题】:Problems with VichUploaderBundle render image using twigVichUploaderBundle 使用 twig 渲染图像的问题
【发布时间】:2015-02-28 03:37:45
【问题描述】:

我在使用 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;
}
}

我使用 {{}} 打印了所有内容,一切正常。

我没有别的办法了。

提前致谢!

【问题讨论】:

  • 能否请您添加产品和照片实体的源代码?
  • 我刚刚添加了两个实体,感谢您花时间阅读和帮助!

标签: php symfony assets vichuploaderbundle


【解决方案1】:

我知道这已经很老了,但对于 Google 来说仍然很重要。我认为这应该可行:

  • 用“imageFile”代替“image”;
  • 类名的斜线加倍;

    {{ vich_uploader_asset(product, 'imageFile', 'AppBundle\\Entity\\Product') }}

【讨论】:

    【解决方案2】:

    这是一个老问题,但这让我以及 Symfony、Doctrine 和 VichUploaderBundle 的新手感到困惑,而且这个问题与 Google 最相关。如果你把这一切都弄错了,一个常见的 Twig 错误是 Mapping not found for field,出于 Google 的原因,我将其包括在内!

    Doctrine 返回一个 Product 对象数组,其中一个 Photo 对象作为每个 Product 的子对象。 vich_uploader_asset 需要这个 Photo 子对象来渲染图像。所以使用dot来访问每个产品对象的照片子对象:product.photo,而不仅仅是product

    vich_uploader_asset 的第二个参数是在您添加 UploadableField 注释/配置的照片实体中使用的字段名称。在上面的示例中,这是imageFile,而不是image。在vich_uploader_asset documentation 中,image 是字段名称,所以这可能是您刚刚复制该代码的位置。

    vich_uploader_asset 的第三个参数是指定可上传实体的类名,同样,它不是产品,而是照片。但实际上你并不需要它,因为只要你指定product.photo,vich 就会发现它。但是,如果您确实需要它,则需要包含 2 个反斜杠,正如 @StefanNch 指出的那样。

    所以这是你需要的行:

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        我终于成功了。 我写了一些 angularjs 代码(我也在我的网站上使用它)。 这就是我所做的

        我的 main.js

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

        routing.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>
        

        我的 config.yml,vich 上传器部分

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

        我不知道这是否是使用捆绑包的好方法(它只是工作),我只是使用 vichUploader 上传图像,然后我进行查询以获取包括文件在内的所有产品名称,一旦我得到它,我就会使用 uri_prefix/{[{product.imageName}]} 来渲染图像。

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

        【讨论】:

          【解决方案5】:

          试试这个,日期是你的对象

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-09
            • 2018-04-04
            • 1970-01-01
            • 2020-01-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多