php - Symfony2 错误的实例通过 Twig 扩展传递

标签 php symfony twig

我创建了一个 Twig 扩展,并在服务中注册了它,但我收到一个错误:

这是扩展:

 <?php
    // src/AppBundle/Twig/AppExtension.php
    namespace Mp\ShopBundle\twig;

    class AppExtension extends \Twig_Extension
    {
        public function getFunctions()
        {
            return array(
                'getTotalPrice'  => new \Twig_Function_Method($this, 'getTotalPrice'));
        }

        public function getTotalPrice(Items $items)
        {
            $total = 0;
            foreach($items as $item){
                $total += $item->getPrice();
            }
            return $total;
        }

        public function getName()
        {
            return 'app_extension';
        }
    }

服务:

services:
    app.twig_extension:
        class: Mp\ShopBundle\twig\AppExtension
        public: false
        tags:
           - { name: twig.extension }

现在我想用我的扩展来计算产品的总和:

 {% for item in product %}

 <td> ${{getTotalPrice(item)}}.00</td>

 {% endfor %}

但是我得到这个错误:

在呈现模板期间抛出异常(“可捕获的 fatal error :传递给 Mp\ShopBundle\twig\AppExtension::getTotalPrice() 的参数 1 必须是 Mp\ShopBundle\twig\Items 的实例,实例给定的 Mp\ShopBundle\Entity\Product,在 C:\wamp\www\Digidis\tree\app\cache\dev\twig\b4\5d\b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php 第 175 行中调用并在 MpFrontShopBundle 中定义"): product_summary.html.twig 第 92 行。

产品:

<?php

namespace Mp\ShopBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Sonata\ClassificationBundle\Model\TagInterface;
use Sonata\ClassificationBundle\Model\Tag;

/**
 * Product
 *
 * @ORM\Table(name="product", indexes={@ORM\Index(name="product_type_id", columns={"product_type_id"})})
 * @ORM\Entity
 */
class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="model", type="string", length=255, nullable=true)
     */
    private $model;

    /**
     * @var \Mp\ShopBundle\Entity\ProductType
     *
     * @ORM\ManyToOne(targetEntity="Mp\ShopBundle\Entity\ProductType")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="product_type_id", referencedColumnName="id")
     * })
     */
    private $productType;

    /**
     * @var \Mp\ShopBundle\Entity\ProductLanguageData
     *
     * @ORM\OneToMany(targetEntity="ProductLanguageData", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id", referencedColumnName="product_id")
     * })
     */
    private $translations;

    /**
     * @var string
     *
     * @ORM\Column(name="admin_title", type="string", length=255, nullable=true)
     */
    private $admin_title;

    protected $tags;

    /**
     * @var \Application\Sonata\ClassificationBundle\Entity\Category
     *
     * @ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Category")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     * })
     */
    private $category;

    /**
     * @var \Application\Sonata\ClassificationBundle\Entity\Category
     *
     * @ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Category")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="subcategory_id", referencedColumnName="id")
     * })
     */
    private $subcategory;

    /**
     * @var \Application\Sonata\ClassificationBundle\Entity\Collection
     *
     * @ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Collection")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id")
     * })
     */
    private $manufacturer;

    /**
     * @var boolean
     *
     * @ORM\Column(name="status", type="boolean")
     */
    private $status;

    /**
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

    /**
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updated_at;

    /**
     * @ORM\Column(name="pc", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $pc;

    /**
     * @ORM\Column(name="value", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $value;

    /**
     * @ORM\Column(name="discount", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $discount;

    /**
     * @ORM\Column(name="base", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $base;

    /**
     * @ORM\Column(name="price", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $price;

    /**
     * @ORM\Column(name="stock", type="integer", nullable=true)
     */
    private $stock;

    /**
     * @ORM\Column(name="map", type="string", length=255, nullable=true)
     */
    private $map;

    /**
     * @ORM\Column(name="feature1", type="string", length=255, nullable=true)
     */
    private $feature1;

    /**
     * @ORM\Column(name="feature2", type="string", length=255, nullable=true)
     */
    private $feature2;

    /**
     * @ORM\Column(name="feature3", type="string", length=255, nullable=true)
     */
    private $feature3;

    /**
     * @ORM\Column(name="feature4", type="string", length=255, nullable=true)
     */
    private $feature4;

    /**
     * @ORM\Column(name="feature5", type="string", length=255, nullable=true)
     */
    private $feature5;

    /**
     * @ORM\Column(name="feature6", type="string", length=255, nullable=true)
     */
    private $feature6;

    /**
     * @ORM\Column(name="feature7", type="string", length=255, nullable=true)
     */
    private $feature7;

    /**
     * @ORM\Column(name="feature8", type="string", length=255, nullable=true)
     */
    private $feature8;

    /**
     * @var boolean
     *
     * @ORM\Column(name="published", type="boolean", nullable=true)
     */
    private $published;

    /**
     * @ORM\Column(name="url_marketing", type="string", length=255, nullable=true)
     */
    private $url_marketing;

    /**
     * @ORM\Column(name="cesion_tienda", type="string", length=255, nullable=true)
     */
    private $cesion_tienda;

    /**
     * @ORM\Column(name="google", type="string", length=255, nullable=true)
     */
    private $google;

    /**
     * @ORM\Column(name="provider_reference", type="string", length=255, nullable=true)
     */
    private $provider_reference;

    private $gallery;

    /**
     * @ORM\Column(name="vat", type="string", length=255, nullable=true)
     */
    private $vat;

    private $provider;

    /**
     * @var string
     *
     * @ORM\Column(name="video1", type="text", length=65535, nullable=true)
     */
    private $video1;

    /**
     * @var string
     *
     * @ORM\Column(name="video2", type="text", length=65535, nullable=true)
     */
    private $video2;

    /**
     * @var string
     *
     * @ORM\Column(name="friendly_url", type="string", length=255, nullable=true)
     */
    private $friendly_url;

    /**
     * @var string
     *
     * @ORM\Column(name="shop_assignment", type="text", length=65535, nullable=true)
     */
    private $shop_assignment;

    /**
     * @var string
     *
     * @ORM\Column(name="custom_product_type", type="string", length=255, nullable=true)
     */
    private $custom_product_type;

    /**
     * Set model
     *
     * @param string $model
     * @return Product
     */
    public function setModel($model)
    {
        $this->model = $model;

        return $this;
    }

    /**
     * Get model
     *
     * @return string 
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set productType
     *
     * @param \Mp\ShopBundle\Entity\ProductType $productType
     * @return Product
     */
    public function setProductType(\Mp\ShopBundle\Entity\ProductType $productType = null)
    {
        $this->productType = $productType;

        return $this;
    }

    /**
     * Get productType
     *
     * @return \Mp\ShopBundle\Entity\ProductType 
     */
    public function getProductType()
    {
        return $this->productType;
    }

    /**
     * Get translations
     *
     * @return \Mp\ShopBundle\Entity\ProductLanguageData
     */
    public function getTranslations()
    {
        return $this->translations;
    }

    /**
     * Set translations
     *
     * @param \Doctrine\ORM\PersistentCollection $translations
     * @return Product
     */
    public function setTranslations(\Doctrine\ORM\PersistentCollection $translations)
    {
        $this->translations = new ArrayCollection();

        foreach ($translations as $s) {
            $this->addTranslation($s);
        }

        return $this;
    }

    /**
     * Add translation
     *
     * @param \Mp\ShopBundle\Entity\ProductLanguageData $translation
     */
    public function addTranslation(\Mp\ShopBundle\Entity\ProductLanguageData $translation)
    {
        $translation->setProduct($this);
        $this->translations[] = $translation;
    }

    /**
     * Remove translation
     *
     * @param \Mp\ShopBundle\Entity\ProductLanguageData $translation
     */
    public function removeTranslation(\Mp\ShopBundle\Entity\ProductLanguageData $translation)
    {
        foreach ($this->translations as $k => $s) {
            if ($s->getId() == $translation->getId()) {
                unset($this->translations[$k]);
            }
        }
    }

    /**
     * Get string value
     *
     * @return string
     */
    public function __toString()
    {
        return ($this->admin_title == "") ? "Product" : $this->admin_title;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        //$this->translations = new \Doctrine\Common\Collections\ArrayCollection();
        $this->tags     = new ArrayCollection();
        $this->status   = false;
    }

    /**
     * Set admin_title
     *
     * @param string $adminTitle
     * @return Product
     */
    public function setAdminTitle($adminTitle)
    {
        $this->admin_title = $adminTitle;

        return $this;
    }

    /**
     * Get admin_title
     *
     * @return string 
     */
    public function getAdminTitle()
    {
        return $this->admin_title;
    }

    /**
     * Add tags
     *
     * @param \Sonata\ClassificationBundle\Model\TagInterface $tags
     */
    public function addTags(TagInterface $tags)
    {
        $this->tags[] = $tags;
    }

    /**
     * Get tags
     *
     * @return array $tags
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * @param $tags
     *
     * @return mixed
     */
    public function setTags($tags)
    {
        $this->tags = $tags;
    }

    /**
     * Set category
     *
     * @param \Application\Sonata\ClassificationBundle\Entity\Category $category
     * @return Product
     */
    public function setCategory(\Application\Sonata\ClassificationBundle\Entity\Category $category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \Application\Sonata\ClassificationBundle\Entity\Category 
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Add tags
     *
     * @param \Application\Sonata\ClassificationBundle\Entity\Tag $tags
     * @return Product
     */
    public function addTag(\Application\Sonata\ClassificationBundle\Entity\Tag $tags)
    {
        $this->tags[] = $tags;

        return $this;
    }

    /**
     * Remove tags
     *
     * @param \Application\Sonata\ClassificationBundle\Entity\Tag $tags
     */
    public function removeTag(\Application\Sonata\ClassificationBundle\Entity\Tag $tags)
    {
        $this->tags->removeElement($tags);
    }

    /**
     * Set subcategory
     *
     * @param \Application\Sonata\ClassificationBundle\Entity\Category $subcategory
     * @return Product
     */
    public function setSubcategory(\Application\Sonata\ClassificationBundle\Entity\Category $subcategory = null)
    {
        $this->subcategory = $subcategory;

        return $this;
    }

    /**
     * Get subcategory
     *
     * @return \Application\Sonata\ClassificationBundle\Entity\Category 
     */
    public function getSubcategory()
    {
        return $this->subcategory;
    }

    /**
     * Set manufacturer
     *
     * @param \Application\Sonata\ClassificationBundle\Entity\Collection $manufacturer
     * @return Product
     */
    public function setManufacturer(\Application\Sonata\ClassificationBundle\Entity\Collection $manufacturer = null)
    {
        $this->manufacturer = $manufacturer;

        return $this;
    }

    /**
     * Get manufacturer
     *
     * @return \Application\Sonata\ClassificationBundle\Entity\Collection 
     */
    public function getManufacturer()
    {
        return $this->manufacturer;
    }

    /**
     * Set status
     *
     * @param boolean $status
     * @return Product
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return boolean 
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return Product
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param \DateTime $updatedAt
     * @return Product
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;

        return $this;
    }

    /**
     * Get updated_at
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Set pc
     *
     * @param string $pc
     * @return Product
     */
    public function setPc($pc)
    {
        $this->pc = $pc;

        return $this;
    }

    /**
     * Get pc
     *
     * @return string 
     */
    public function getPc()
    {
        return $this->pc;
    }

    /**
     * Set value
     *
     * @param string $value
     * @return Product
     */
    public function setValue($value)
    {
        $this->value = $value;

        return $this;
    }

    /**
     * Get value
     *
     * @return string 
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Set discount
     *
     * @param string $discount
     * @return Product
     */
    public function setDiscount($discount)
    {
        $this->discount = $discount;

        return $this;
    }

    /**
     * Get discount
     *
     * @return string 
     */
    public function getDiscount()
    {
        return $this->discount;
    }

    /**
     * Set base
     *
     * @param string $base
     * @return Product
     */
    public function setBase($base)
    {
        $this->base = $base;

        return $this;
    }

    /**
     * Get base
     *
     * @return string 
     */
    public function getBase()
    {
        return $this->base;
    }

    /**
     * Set price
     *
     * @param string $price
     * @return Product
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return string 
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set stock
     *
     * @param string $stock
     * @return Product
     */
    public function setStock($stock)
    {
        $this->stock = $stock;

        return $this;
    }

    /**
     * Get stock
     *
     * @return string 
     */
    public function getStock()
    {
        return $this->stock;
    }

    /**
     * Set map
     *
     * @param string $map
     * @return Product
     */
    public function setMap($map)
    {
        $this->map = $map;

        return $this;
    }

    /**
     * Get map
     *
     * @return string 
     */
    public function getMap()
    {
        return $this->map;
    }

    /**
     * Set feature1
     *
     * @param string $feature1
     * @return Product
     */
    public function setFeature1($feature1)
    {
        $this->feature1 = $feature1;

        return $this;
    }

    /**
     * Get feature1
     *
     * @return string 
     */
    public function getFeature1()
    {
        return $this->feature1;
    }

    /**
     * Set feature2
     *
     * @param string $feature2
     * @return Product
     */
    public function setFeature2($feature2)
    {
        $this->feature2 = $feature2;

        return $this;
    }

    /**
     * Get feature2
     *
     * @return string 
     */
    public function getFeature2()
    {
        return $this->feature2;
    }

    /**
     * Set feature3
     *
     * @param string $feature3
     * @return Product
     */
    public function setFeature3($feature3)
    {
        $this->feature3 = $feature3;

        return $this;
    }

    /**
     * Get feature3
     *
     * @return string 
     */
    public function getFeature3()
    {
        return $this->feature3;
    }

    /**
     * Set feature4
     *
     * @param string $feature4
     * @return Product
     */
    public function setFeature4($feature4)
    {
        $this->feature4 = $feature4;

        return $this;
    }

    /**
     * Get feature4
     *
     * @return string 
     */
    public function getFeature4()
    {
        return $this->feature4;
    }

    /**
     * Set feature5
     *
     * @param string $feature5
     * @return Product
     */
    public function setFeature5($feature5)
    {
        $this->feature5 = $feature5;

        return $this;
    }

    /**
     * Get feature5
     *
     * @return string 
     */
    public function getFeature5()
    {
        return $this->feature5;
    }

    /**
     * Set feature6
     *
     * @param string $feature6
     * @return Product
     */
    public function setFeature6($feature6)
    {
        $this->feature6 = $feature6;

        return $this;
    }

    /**
     * Get feature6
     *
     * @return string 
     */
    public function getFeature6()
    {
        return $this->feature6;
    }

    /**
     * Set feature7
     *
     * @param string $feature7
     * @return Product
     */
    public function setFeature7($feature7)
    {
        $this->feature7 = $feature7;

        return $this;
    }

    /**
     * Get feature7
     *
     * @return string 
     */
    public function getFeature7()
    {
        return $this->feature7;
    }

    /**
     * Set feature8
     *
     * @param string $feature8
     * @return Product
     */
    public function setFeature8($feature8)
    {
        $this->feature8 = $feature8;

        return $this;
    }

    /**
     * Get feature8
     *
     * @return string 
     */
    public function getFeature8()
    {
        return $this->feature8;
    }

    /**
     * Set published
     *
     * @param boolean $published
     * @return Product
     */
    public function setPublished($published)
    {
        $this->published = $published;

        return $this;
    }

    /**
     * Get published
     *
     * @return boolean 
     */
    public function getPublished()
    {
        return $this->published;
    }

    /**
     * Set url_marketing
     *
     * @param string $urlMarketing
     * @return Product
     */
    public function setUrlMarketing($urlMarketing)
    {
        $this->url_marketing = $urlMarketing;

        return $this;
    }

    /**
     * Get url_marketing
     *
     * @return string 
     */
    public function getUrlMarketing()
    {
        return $this->url_marketing;
    }

    /**
     * Set cesion_tienda
     *
     * @param string $cesionTienda
     * @return Product
     */
    public function setCesionTienda($cesionTienda)
    {
        $this->cesion_tienda = $cesionTienda;

        return $this;
    }

    /**
     * Get cesion_tienda
     *
     * @return string 
     */
    public function getCesionTienda()
    {
        return $this->cesion_tienda;
    }

    /**
     * Set google
     *
     * @param string $google
     * @return Product
     */
    public function setGoogle($google)
    {
        $this->google = $google;

        return $this;
    }

    /**
     * Get google
     *
     * @return string 
     */
    public function getGoogle()
    {
        return $this->google;
    }

    /**
     * Set provider_reference
     *
     * @param string $providerReference
     * @return Product
     */
    public function setProviderReference($providerReference)
    {
        $this->provider_reference = $providerReference;

        return $this;
    }

    /**
     * Get provider_reference
     *
     * @return string 
     */
    public function getProviderReference()
    {
        return $this->provider_reference;
    }

    /**
     * Set gallery
     *
     * @param \Application\Sonata\MediaBundle\Entity\Gallery $gallery
     * @return Product
     */
    public function setGallery(\Application\Sonata\MediaBundle\Entity\Gallery $gallery = null)
    {
        $this->gallery = $gallery;

        return $this;
    }

    /**
     * Get gallery
     *
     * @return \Application\Sonata\MediaBundle\Entity\Gallery 
     */
    public function getGallery()
    {
        return $this->gallery;
    }

    /**
     * Set vat
     *
     * @param string $vat
     * @return Product
     */
    public function setVat($vat)
    {
        $this->vat = $vat;

        return $this;
    }

    /**
     * Get vat
     *
     * @return string 
     */
    public function getVat()
    {
        return $this->vat;
    }

    /**
     * Set provider
     *
     * @param \Application\Sonata\UserBundle\Entity\User $provider
     * @return Product
     */
    public function setProvider(\Application\Sonata\UserBundle\Entity\User $provider = null)
    {
        $this->provider = $provider;

        return $this;
    }

    /**
     * Get provider
     *
     * @return \Application\Sonata\UserBundle\Entity\User 
     */
    public function getProvider()
    {
        return $this->provider;
    }

    /**
     * Set video1
     *
     * @param string $video1
     * @return Product
     */
    public function setVideo1($video1)
    {
        $this->video1 = $video1;

        return $this;
    }



    /**
     * Get custom_product_type
     *
     * @return string 
     */
    public function getCustomProductType()
    {
        return $this->custom_product_type;
    }
}

那么出于某种原因它传递了错误的类?如何解决?

最佳答案

是的,您在 twig 模板中传递了错误的类。

这部分代码:

{% for item in product %}
  <td>${{getTotalPrice(item)}}.00</td>
{% endfor %}

您正在传递 item 参数,它是 Product 类的实例。

您应该重写您的 Twig 扩展以接受 Product 实例:

public function getTotalPrice($items)
{
    $total = 0;
    foreach($items as $item){
        $total += $item->getPrice();
    }
    return $total;
}

然后传递整个数组,而不是只有一个Product:

<td>${{getTotalPrice(product)}}.00</td>

希望这对您有所帮助!

关于php - Symfony2 错误的实例通过 Twig 扩展传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843333/

相关文章:

php - preload.php 文件在哪里,它是如何生成的?

php - 当我将MySQL数据库转换为JSON时,为什么第一项是空数组?

php - 检测 YouTube channel 是否存在,如果出现错误则不显示?

php - 在 URL 中使用 & 会使 $_GET 出错

php - 在 Symfony2 中为电子邮件模板动态生成内联 css 样式

php - 向 Timber/Twig 添加自定义随机播放过滤器

php - 在symfony2项目中使用elasticsearch进行范围过滤查询

php - Doctrine ODM如何随机选择?

symfony - Doctrine2 "order by"在 "group by"之前

mongodb - 如何在 symfony2 twig 模板中显示 mongodb 日期?