简体   繁体   中英

Doctrine2 / Symfony2 --> sort result using finders

Using Doctrine2 and Symfony2, how do we get directly sorted the result of a query?

You know that Symfony has this funny way to get data:

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

This would result on an array with all the objects fitting that $criteria.

But what if I want to get the results directly sorted by some $sorting_criteria?

Isn't there anything like:

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

Ok, got it.

Just use QueryBuilder.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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