findOneBy
findOneBy is a method commonly used in Doctrine ORM repositories to fetch a single entity that matches a set of field-value criteria. It is part of the repository interface and provides a convenient way to perform simple lookups without writing a full query. The method signature is findOneBy(array $criteria, array $orderBy = null).
Criteria is an associative array where keys are entity field names and values are the values to
The method returns the matching entity or null if none exists. This makes findOneBy convenient for unique
- $user = $entityManager->getRepository(User::class)->findOneBy(['email' => '[email protected]']);
- $latestPost = $entityManager->getRepository(Post::class)->findOneBy(['author' => $author, 'status' => 'published'], ['publishedAt' => 'DESC']);
Relation to other repository methods: findOneBy differs from findBy in that it returns a single entity (or