1

我的项目中有 Book、User 和 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 %}
4

1 回答 1

0

将您的集合传递给 twig 模板后,假设变量名称为 results,并且您的 Cart 实体有一个 getBook 方法或 book 属性是公共的:

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

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

于 2013-02-17T18:23:33.437 回答