gpt4 book ai didi

php - Symfony/Doctrine - createQueryBuilder orderBy

转载 作者:可可西里 更新时间:2023-11-01 00:58:00 24 4
gpt4 key购买 nike

我有一个“团队”实体,其属性为“预算”。我只想打印团队属性,我希望预算最高的团队出现在第一位、第二位、第三位...(DESC)。

但是使用这段代码,它不起作用,我不明白为什么。

indexAction( Controller )

    $em = $this->getDoctrine()->getManager();
$teams = $em->getRepository('FootballBundle:Team')->getAllTeamsSortedByDescBudget();


return $this->render('FootballBundle:Default:index.html.twig', array(
'teams' => $teams,
));

团队资源库

public function getAllTeamsSortedByDescBudget()
{
$q = $this->createQueryBuilder('a');
$q->select()->from('FootballBundle:Team', 't')->orderBy('t.budget', 'DESC');

return $q->getQuery()->getResult();
}

Twig View

<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£

切尔西...哈哈

编辑:更正!查看接受评论。

最佳答案

getAllTeamsSortedByDescBudget 方法中的 QueryBuilder 查询更改为:

public function getAllTeamsSortedByDescBudget() 
{
$qb = $this->createQueryBuilder('t')
->orderBy('t.budget', 'DESC');

return $qb->getQuery()->getResult();
}

关于php - Symfony/Doctrine - createQueryBuilder orderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com