Symfony/Doctrine - createQueryBuilder orderBy(Symfony/Doctrine - createQueryBuilder orderBy)
问题描述
我有一个带有属性预算"的团队"实体.我只想打印团队属性,我希望预算最大的团队出现在第一、第二、第三......(DESC).
I have a 'Team' Entity with a property 'budget'. I just want to print the teams properties and i want that the team with the biggest budget appear in first position, the second, the third... (DESC).
但是使用此代码,它不起作用,我不明白为什么.
But with this code, it does not work and i don't understand why.
indexAction(控制器)
$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();
}
树枝视图
<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
*
* @ORMColumn(name="budget", type="integer")
*/
private $budget;
这里,结果......
And here, the result ...
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 评论.
EDIT : CORRECTED ! See the takeit comment.
推荐答案
将 getAllTeamsSortedByDescBudget
方法中的 QueryBuilder
查询更改为:
Change your QueryBuilder
query in getAllTeamsSortedByDescBudget
method to:
public function getAllTeamsSortedByDescBudget()
{
$qb = $this->createQueryBuilder('t')
->orderBy('t.budget', 'DESC');
return $qb->getQuery()->getResult();
}
这篇关于Symfony/Doctrine - createQueryBuilder orderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony/Doctrine - createQueryBuilder orderBy
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01