2013-02-17 64 views
1

我在我的项目中拥有Book,User和Cart实体。我需要从Cart实体中获得3本最受欢迎的书籍。 我在仓库以下查询:TWIG,显示GROUPED BY查询结果

query = $this->getEntityManager() 
->createQuery 
("SELECT c, (COUNT(c.book)) AS total 
    FROM ValinorBookStoreBundle:Cart c 
    JOIN c.book b 
    GROUP BY c.book 
    ORDER BY total DESC") 
->setMaxResults(3); 

该查询返回我下面的阵列(如显示的var_dump):

array (size=4) 
    0 => 
    array (size=2) 
     0 => 
     object(Valinor\BookStoreBundle\Entity\Cart)[1021] 
      private 'id' => int 14 
      protected 'book' => 
      object(Valinor\BookStoreBundle\Entity\Book)[894] 
       ... 
      protected 'user' => 
      object(Proxies\__CG__\Valinor\BookStoreBundle\Entity\User)[1104] 
       ... 
      protected 'datum' => 
      object(DateTime)[1041] 
       ... 
      protected 'billNumber' => int 1 
     'total' => string '3' (length=1) 
    1 => 
    array (size=2) 
     0 => 
     object(Valinor\BookStoreBundle\Entity\Cart)[1100] 
      private 'id' => int 13 
      protected 'book' => 
      object(Valinor\BookStoreBundle\Entity\Book)[1002] 
       ... 
      protected 'user' => 
      object(Proxies\__CG__\Valinor\BookStoreBundle\Entity\User)[1104] 
       ... 
      protected 'datum' => 
      object(DateTime)[1101] 
       ... 
      protected 'billNumber' => int 1 
     'total' => string '2' (length=1) 
    2 => 
    array (size=2) 
     0 => 
     object(Valinor\BookStoreBundle\Entity\Cart)[1098] 
      private 'id' => int 15 
      protected 'book' => 
      object(Valinor\BookStoreBundle\Entity\Book)[930] 
       ... 
      protected 'user' => 
      object(Proxies\__CG__\Valinor\BookStoreBundle\Entity\User)[1104] 
       ... 
      protected 'datum' => 
      object(DateTime)[1099] 
       ... 
      protected 'billNumber' => int 1 
     'total' => string '1' (length=1) 
    3 => 
    array (size=2) 
     0 => 
     object(Valinor\BookStoreBundle\Entity\Cart)[1096] 
      private 'id' => int 16 
      protected 'book' => 
      object(Valinor\BookStoreBundle\Entity\Book)[948] 
       ... 
      protected 'user' => 
      object(Proxies\__CG__\Valinor\BookStoreBundle\Entity\User)[1095] 
       ... 
      protected 'datum' => 
      object(DateTime)[1097] 
       ... 
      protected 'billNumber' => int 2 
     'total' => string '1' (length=1) 

我的问题是如何在TWIG显示图书的属性?

这里是车的实体:

<?php 

namespace Valinor\BookStoreBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Cart 
* 
* @ORM\Table(name="cart") 
* @ORM\Entity(repositoryClass="Valinor\BookStoreBundle\Entity\CartRepository") 
*/ 
class Cart 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Book", inversedBy="cart") 
    **/ 
    protected $book; 

    /** 
    * @ORM\ManyToOne(targetEntity="User", inversedBy="carts") 
    **/ 
    protected $user; 

    /** 
    * @var datetime $datum 
    * 
    * @ORM\Column(name="datum", type="datetime", unique=false, nullable=true) 
    * 
    */ 
    protected $datum; 

    /** 
    * @var integer $billNumber 
    * 
    * @ORM\Column(name="billNumber", type="integer", unique=false, nullable=false) 
    * 
    */ 
    protected $billNumber; 

    public function __construct() 
    { 
     $this->datum = new \DateTime(); 
    } 

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

    /** 
    * Set datum 
    * 
    * @param \DateTime $datum 
    * @return Cart 
    */ 
    public function setDatum($datum) 
    { 
     $this->datum = $datum; 

     return $this; 
    } 

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

    /** 
    * Set billNumber 
    * 
    * @param integer $billNumber 
    * @return Cart 
    */ 
    public function setBillNumber($billNumber) 
    { 
     $this->billNumber = $billNumber; 

     return $this; 
    } 

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

    /** 
    * Set book 
    * 
    * @param \Valinor\BookStoreBundle\Entity\Book $book 
    * @return Cart 
    */ 
    public function setBook(\Valinor\BookStoreBundle\Entity\Book $book = null) 
    { 
     $this->book = $book; 

     return $this; 
    } 

    /** 
    * Get book 
    * 
    * @return \Valinor\BookStoreBundle\Entity\Book 
    */ 
    public function getBook() 
    { 
     return $this->book; 
    } 

    /** 
    * Set user 
    * 
    * @param \Valinor\BookStoreBundle\Entity\User $user 
    * @return Cart 
    */ 
    public function setUser(\Valinor\BookStoreBundle\Entity\User $user = null) 
    { 
     $this->user = $user; 

     return $this; 
    } 

    /** 
    * Get user 
    * 
    * @return \Valinor\BookStoreBundle\Entity\User 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 
} 

解决: 下面是该查询:

//most popular books 
     $popularBooks = $this->getDoctrine() 
          ->getRepository('ValinorBookStoreBundle:Cart') 
          ->findMostPopularBooks(); 

     $results = array(); 
     foreach($popularBooks as $key=>$el) 
     { 
      $results[] = $el['0']; 
     } 

这是如何ü使用它的树枝:

{% for result in results %} 
     <table> 
      <tr> 
       <td> 
        <img class="cover" src="/Symfony2/web/bundles/valinorbookstore/images/{{result.book.isbn}}.jpg"/> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        {% for author in result.book.authors %} 
         {{author}} 
         <br /> 
        {% endfor%} 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <a href="{{ path('showBook', { 'id': result.book.id }) }}" style="text-decoration: none"> 
         ,, {{result.book.name}} ,, 
        </a> 
       </td> 
      </tr> 
     </table> 
     <hr /> 
    {% endfor %} 

回答

0

传球后你的集合到树枝模板,假设变量名是结果,并且你的Cart实体有一个getBook方法或书本属性是公共的:

{% for result in results %} 
    {{ result.book.foo }} 
{% endfor %} 

http://twig.sensiolabs.org/doc/templates.html

+0

我已经试过了,但它不工作。我可以得到,, total ,,属性,但我不能从数组中获取对象。我现在添加了购物车和本书的答案。 – Wasyster 2013-02-18 13:14:49