简体   繁体   English

Doctrine2 / Symfony2->使用查找器对结果进行排序

[英]Doctrine2 / Symfony2 --> sort result using finders

Using Doctrine2 and Symfony2, how do we get directly sorted the result of a query? 使用Doctrine2和Symfony2,我们如何直接对查询结果进行排序?

You know that Symfony has this funny way to get data: 您知道Symfony有这种有趣的方式来获取数据:

$array_objects = $repository->findAllByCriteria($criteria);

This would result on an array with all the objects fitting that $criteria. 这将导致一个数组,其中所有对象都符合$ criteria。

But what if I want to get the results directly sorted by some $sorting_criteria? 但是,如果我想直接将结果按$ sorting_criteria排序怎么办?

Isn't there anything like: 没有什么类似的东西:

$sorted_array_objects = $repository->findAllByCriteria($criteria)->sort($sorting_criteria);

Ok, got it. 好的,我知道了。

Just use QueryBuilder. 只需使用QueryBuilder。

$arr_products = $pack_repo->createQueryBuilder('p')
    ->where('p.active = true')
    ->orderBy('p.rating', 'DESC')
    ->getQuery()
    ->getResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM