如何在树枝模板中显示已连接的实体列

时间:2013-08-23 18:00:57

标签: symfony join doctrine entity twig

我不知道我错过了什么,但我一直在与Twig战斗,无法找出我在哪里错误。

我有三个实体:Product,ProductImage和Category

每个产品作为一个类别和一个类别有许多产品。 每件产品都有很多图像。

我能够列出图像集,这不是问题。

问题是我无法获取Twig模板文件中每个产品的类别说明( product.categories.description ),原因是:< / p>

{% extends '::base.html.twig' %}
{% block body %}
<div id="vitrines">
    {{ debug products }}
    {% for product in products %}
        <div class="boxVitrine">
            {% for image in product.images %}
                {% if(image.oneOfType == 'thumb') %}
                    <img src="{{ asset('bundles/artevitrinesite/products/') }}{{ image.filename }}" />
                {% endif %}
            {% endfor %}
            <div class="info">
                <div class="container">
                    <div class="top">
                        <div class="code">
                            Cod: <span class="red1"><strong>{{ product.code }}</strong></span>
                            <br />{{ product.categories.description }}
                        </div>
                        <div class="price">{{ product.price | bigprice | raw }}</div>
                    </div>
                </div>
            </div>
        </div>

    {% endfor %}
</div>
{%endblock%}

所以,我有这两个类:

class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @see Entity/Category
     * 
     * @var integer $categoryId
     * 
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *
     */    
    protected $categoryId;


    /**
     * 
     * @var string $title
     * 
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     * 
     */
    protected $title;        


    /**
     * 
     * @var string $description
     * 
     * @ORM\Column(name="description", type="text", nullable=false )
     * 
     */      
    protected $description;

    /**
     * 
     * @var string $price
     * 
     * @ORM\Column(name="price", type="decimal", precision=9, scale=2, nullable=false)
     * 
     */      
    protected $price;


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

    /**
     * 
     * @var string $createdOn
     * 
     * @ORM\Column(name="created_on", type="datetime")
     * 
     */      
    protected $createdOn;


    /**
     * @var \Doctrine\Common\Collections\ArrayCollection $images
     * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="productId")
     *
     */
    protected $images;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * 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 createdOn
     *
     * @param \DateTime $createdOn
     * @return Product
     */
    public function setCreatedOn($createdOn)
    {
        $this->createdOn = $createdOn;

        return $this;
    }

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

    /**
     * Set categoryId
     *
     * @param Category $categoryId
     * @return Product
     */
    public function setCategoryId(Category $categoryId = null)
    {
        $this->categoryId = $categoryId;

        return $this;
    }

    /**
     * Get categoryId
     *
     * @return Category 
     */
    public function getCategoryId()
    {
        return $this->categoryId;
    }

    /**
     * Add images
     *
     * @param \ArteVitrine\BackendBundle\Entity\ProductImage $images
     * @return Product
     */
    public function addImage(ProductImage $images)
    {
        $this->images[] = $images;

        return $this;
    }

    /**
     * Remove images
     *
     * @param ProductImage $images
     */
    public function removeImage(ProductImage $images)
    {
        $this->images->removeElement($images);
    }

    /**
     * Get images
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getImages()
    {
        return $this->images;
    }
}

class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * 
     * @var string $description
     *
     * @ORM\Column(name="description", type="text", nullable=false )
     *
     */
    protected $description;

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

    /**
     * 
     * @var ArrayCollection $products
     * 
     * @ORM\OneToMany(targetEntity="Category", mappedBy="categoryId")
     *
     */
    protected $products;

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

    /**
     * Set description
     *
     * @param string $description
     * @return Category
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * Add products
     *
     * @param Category $products
     * @return Category
     */
    public function addProduct(Category $products)
    {
        $this->products[] = $products;

        return $this;
    }

    /**
     * Remove products
     *
     * @param Category $products
     */
    public function removeProduct(Category $products)
    {
        $this->products->removeElement($products);
    }

    /**
     * Get products
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getProducts()
    {
        return $this->products;
    }
}

到目前为止,非常好,我只是不知道如何在循环中显示每个产品的类别信息,也不知道我是否以正确的方式进行。

提前致谢。

1 个答案:

答案 0 :(得分:7)

首先,根据您描述的实体,您应该替换

product.categories.description 

通过

product.category.description 

“每个产品都有一个类别”

然后,要运行此调用,您应该在Product实体中将$ categoryId替换为$ category,如下所示,

   /**
     * @see Entity/Category
     * 
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *
     */    
    protected $category;