How to sort findAll Doctrine's method?

前端 未结 12 2162
闹比i
闹比i 2020-12-23 00:14

I\'ve been reading Doctrine\'s documentation, but I haven\'t been able to find a way to sort findAll() Results.

I\'m using symfony2 + doctrine, this is the statemen

12条回答
  •  长情又很酷
    2020-12-23 01:06

    You can sort an existing ArrayCollection using an array iterator.

    assuming $collection is your ArrayCollection returned by findAll()

    $iterator = $collection->getIterator();
    $iterator->uasort(function ($a, $b) {
        return ($a->getPropery() < $b->getProperty()) ? -1 : 1;
    });
    $collection = new ArrayCollection(iterator_to_array($iterator));
    

    This can easily be turned into a function you can put into your repository in order to create findAllOrderBy() method.

提交回复
热议问题