3
votes

I created a Twig extension, registered it in services, but im getting an error:

This is the extension:

 <?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:

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

Now i want to count the sum of products with my extension like this:

 {% for item in product %}

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

 {% endfor %}

But i get this error:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Mp\ShopBundle\twig\AppExtension::getTotalPrice() must be an instance of Mp\ShopBundle\twig\Items, instance of Mp\ShopBundle\Entity\Product given, called in C:\wamp\www\Digidis\tree\app\cache\dev\twig\b4\5d\b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php on line 175 and defined") in MpShopBundle:Frontend:product_summary.html.twig at line 92.

Product:

<?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;
    }
}

So for some reason it is passing the wrong class? How can this be fixed?

2
Can you show your Product entity?Matheno
Updated my question with productDominykas55
Hm, I was expecting to find an instance of Item in your Product entity but I can't find it. You're passing item from product, but since I can't find this instance of it in product, it will be an instance of Product I suppose.Matheno

2 Answers

0
votes

If you want pass a reference of you Product object (entity), you have to inform it to your method:

use Mp\ShopBundle\Entity\Product; - src/AppBundle/Twig/AppExtension.php

And then change your Items to Product object:

...
public function getTotalPrice(Product $items) {
....
0
votes

Yes, you are passing the wrong class in your twig template.

In this part of code:

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

You are passing the item parameter which is the instance of Product class.

You should rewrite your Twig extension to accept Product instanses:

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

And then pass the whole array, not only one Product:

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

Hope this helps!