Querying
RepositoryInterface defines the following operations. Examples in this guide use the in-memory Repository; custom implementations execute their own backend queries.
| Method | Result |
|---|---|
find($id) |
Entity object or null |
findAll() |
Array of entity objects |
findBy($criteria, $orderBy, $limit, $offset) |
Array of matching entity objects |
findOneBy($criteria, $orderBy) |
First matching entity or null |
count($criteria) |
Number of matching rows |
matching($criteria) |
Collection of entity objects selected with Doctrine Criteria |
findByCriteria($criteria) |
The same entity objects as an array |
getClassName() |
Entity class name |
In a repository typed with an entity (RepositoryInterface<Product>), “entity object” is an instance of that entity. See typed repositories.
Identifier Lookup
$product = $repository->find('prod-001');
$missing = $repository->find('unknown'); // null.
The in-memory implementation accepts string and integer identifiers and looks up the stored row key. Its optional find() lock arguments are unused. An id field inside the row does not change the lookup key.
Filtering and Counting
$computers = $repository->findBy(['category' => 'computers']);
$selected = $repository->findBy([
'category' => ['computers', 'accessories'],
]);
$first = $repository->findOneBy(['category' => 'computers']);
$total = $repository->count();
$computerCount = $repository->count(['category' => 'computers']);
Fields in a criteria array are combined with AND. An array value means any of the listed values may match that field. The in-memory implementation compares values with PHP’s non-strict in_array().
A row that does not have a field has it as null. A null value matches the rows whose field is null, and a list that contains null matches them too. Any other value never matches a null field, so 0, '' and false are not null.
A field that no row has is rejected with a translatable invalid-argument exception. A repository without rows has no fields to check and accepts any field.
An empty criteria array matches every row. findAll() and the filtering methods return lists of entities rather than preserving source keys.
Ordering and Pagination
$page = $repository->findBy(
criteria: ['category' => 'computers'],
orderBy: ['price' => 'DESC', 'name' => 'ASC'],
limit: 20,
offset: 40,
);
The in-memory implementation processes ordering fields in the supplied order. A direction is ASC or DESC, in any case and with spaces around it. Any other direction, a direction that is not a string, or a field that no row has raises a translatable invalid-argument exception.
A null or missing field is the smallest value: it goes first when ascending and last when descending. Rows that tie keep their relative order.
An offset or limit below zero raises a translatable invalid-argument exception.
Doctrine Criteria
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
$expensiveProducts = $repository->matching(
Criteria::create()
->where(Criteria::expr()->gt('price', 1000))
->orderBy(['price' => Order::Descending])
->setMaxResults(10),
);
RepositoryInterface extends Doctrine Collections’ Selectable. matching() returns a collection of entity objects whose keys are their positions in the result, and findByCriteria() returns the same objects as an array.
For Repository, the criteria are evaluated over the stored rows before entities are created, with the semantics of a database:
- A missing field is null.
- A comparison with a null value is not true.
eq(field, null)andisNull()ask for null, andneq(field, null)andisNotNull()ask for not null. not()leaves out the rows whose answer is unknown, asneq()andnotIn()leave out null fields.in()with a list that contains null also matches null fields.- The text operators
contains(),startsWith()andendsWith()ignore the case of ASCII letters. The other letters must be the same. memberOf()looks inside a field that holds a list.- A field with dots, such as
address.city, reads inside the arrays of the row. - A maximum of zero results is an empty result.
The supported operators are eq, neq, lt, lte, gt, gte, in, notIn, contains, startsWith, endsWith, memberOf, isNull, isNotNull and the compositions andX, orX and not. A field that no row has, or an operator that is not supported, raises a translatable invalid-argument exception.
The evaluation is implemented by CriteriaEvaluator through CriteriaEvaluatorInterface. Repository accepts another evaluator as its last constructor argument.
A custom SQL repository is responsible for implementing these query methods and translating supported criteria to its backend. The manager does not translate expressions or issue SQL.
Differences from a Database
The package tests the in-memory Repository against Doctrine’s repository over a SQLite table with the same data. They answer alike except where a repository over rows has no schema or deliberately differs:
- The fields of a repository are the keys of its rows. A repository without rows accepts any field, and a field that no row has is unknown even if the entity class declares it.
- The text of
contains(),startsWith()andendsWith()is searched as it is. The characters%and_are not wildcards. - A limit below zero raises an exception, whereas SQLite takes it as no limit.
Raw Rows and Entities
Repository::all() returns raw rows, while Repository::findAll() returns entities. The rows of a repository cannot be modified through it, and it does not write to a source file or database. Each row must be an array.