2013-08-23 65 views
0

我不知道我在想什么,但我一直在与树枝战斗,无法找到我错在哪里。如何在树枝模板中显示加入的实体列

我有三个实体:产品,ProductImage和类别

每一件产品在同一个类别和一个类别有很多产品。 每个产品都有很多图片。

我可以列出图像集合,这不是问题。

的问题是,我只是不能在我的枝条模板文件中的每个产品类别描述(product.categories.description),如:

{% 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; 
    } 
} 

到目前为止,这么好,我只是不知道如何显示循环内每个产品的类别信息,也不知道如何以正确的方式进行操作。

在此先感谢。

+0

在'Product'中重命名'$ categoryId' - >'$ category'(和setter/getter),然后在树枝中:'{{product.category.description}}' – cheesemacfly

回答

7

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

product.categories.description 

通过

product.category.description 

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

然后,让这个调用运行,您应该在产品实体中用$ category替换$ categoryId,如下所示:

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

谢谢,工作起来像一个魅力。事实上,我没有正确使用! –