我的项目中有Book,User和Cart实体。我需要从Cart实体中获得3本最受欢迎的书籍。 我在存储库中有以下查询:
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'];
}
这就是你在Twig中使用它的方式:
{% 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 :(得分:0)
将您的集合传递给twig模板后,假设变量名称是结果,并且您的Cart实体具有getBook方法或book属性是公共的:
{% for result in results %}
{{ result.book.foo }}
{% endfor %}