2013-02-17 66 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