doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 504 forks source link

Support OrderBy / Sort for DocumentRepository findOneBy() #2226

Closed nottoohigh closed 3 years ago

nottoohigh commented 4 years ago

Feature Request

Q A
New Feature yes
BC Break no
Version Since 2.1

Summary

Unlike findBy, Doctrine\ODM\MongoDB\Repository\DocumentRepository::findOneBy does not currently support $sort as a parameter.

Current

 /**
   * Finds a single document by a set of criteria.
 */
public function findOneBy(array $criteria) : ?object
{
  return $this->getDocumentPersister()->load($criteria);
}

Propose adding support for $sort:

/**
   * Finds a single document by a set of criteria.
*/
public function findOneBy(array $criteria, ?array $sort = null) : ?object
 {
     return $this->getDocumentPersister()->load($criteria, null, [], 0, $sort);
 }
malarzm commented 4 years ago

@nottoohigh sounds reasonable :) would you mind sending us a PR with the change and some tests?

nottoohigh commented 4 years ago

@malarzm Ok will do as soon as I can. Thank you!

malarzm commented 3 years ago

Closing as https://github.com/doctrine/mongodb-odm/pull/2264 was just merged