I have a question about the order in the pagination in Symfony 2.7.
Before we used pagination we used the PHP function usort to sort some things. But my question now is how could we implement the usort in the doctrine query with the same order like the usort. Which needs to be working with the Paginator. Since when we use now the query (here under) we don't get the proper results.
usort function:
usort($result, function ($a, $b) {
$aBegin = $a->getStartDate() ?: $a->getCreatedDate();
$bBegin = $b->getStartDate() ?: $b->getCreatedDate();
if ($aBegin < $bBegin) {
return -1;
}
if ($aBegin == $bBegin) {
return 0;
}
if ($aBegin > $bBegin) {
return 1;
}
return 0;
});
How could we implemented the usort in the following query:
$build = $this->createQueryBuilder('building');
$build
->addSelect('users', 'furniture')
->join('building.users', 'users')
->leftJoin('building.furniture', 'furniture')
->where('building.id = :id')
->setParameter('id', $id)
->orderBy('building.getStartDate', 'ASC')
->addOrderBy('building.getCreatedDate', 'DESC');
$paginator = new Paginator($build->getQuery(), $fetchJoinCollection = true);
$result = $paginator->getQuery()
->setFirstResult($offset)
->setMaxResults($limit)
->getResult();
Thanks!
Doctrine orm: 2.2.3, Symfony version: 2.7