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 (controller)
$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();
}
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 entity
/**
* @var integer
*
* @ORM\Column(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£
Chelsea... lol
EDIT : CORRECTED ! See the takeit comment.
Change your QueryBuilder
query in getAllTeamsSortedByDescBudget
method to:
public function getAllTeamsSortedByDescBudget()
{
$qb = $this->createQueryBuilder('t')
->orderBy('t.budget', 'DESC');
return $qb->getQuery()->getResult();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With