3
我有一個'團隊'實體與財產'預算'。我只想打印球隊屬性,我希望擁有最大預算的球隊出現在第一名,第二名,第三名......(DESC)。Symfony/Doctrine - createQueryBuilder orderBy
但使用此代碼,它不起作用,我不明白爲什麼。
的indexAction(控制器)
$em = $this->getDoctrine()->getManager();
$teams = $em->getRepository('FootballBundle:Team')->getAllTeamsSortedByDescBudget();
return $this->render('FootballBundle:Default:index.html.twig', array(
'teams' => $teams,
));
TeamRepository
public function getAllTeamsSortedByDescBudget()
{
$q = $this->createQueryBuilder('a');
$q->select()->from('FootballBundle:Team', 't')->orderBy('t.budget', 'DESC');
return $q->getQuery()->getResult();
}
樹枝視圖
<h1>Teams list</h1>
<ul>
{% for team in teams %}
<li>{{ team.name }} - {{ team.championship }} - {{ team.budget|number_format(2, '.', ',') }}£</li>
{% endfor %}
<br/>
</ul>
Team.php實體
/**
* @var integer
*
* @ORM\Column(name="budget", type="integer")
*/
private $budget;
這裏,結果......
Teams list
Manchester City FC - Premier League - 100,000,000.00£
Arsenal FC - Premier League - 50,000,000.00£
Leicester City - Premier League - 20,000,000.00£
Crystal Palace FC - Premier League - 5,000,000.00£
Chelsea FC - Premier League - 100,000,000.00£
切爾西...笑
編輯:更正!請參閱takeit評論。
如何你'budget'屬性和映射看起來像在實體? – takeit
/** * @var整數 * * @ORM \ Column(name =「budget」,type =「integer」) */ private $ budget; – KitchenLee
首先嚐試:'public function getAllTeamsSortedByDescBudget() { $ q = $ this-> createQueryBuilder('t') - > orderBy('t.budget','DESC'); return $ q-> getQuery() - > getResult(); }' – takeit