Skip to content

Repository API

wind111-lang edited this page Jul 9, 2026 · 1 revision

Repository API

Sumire\EntityRepository provides a typed helper around Database for one entity class.

You usually create it through Database::repository().

$users = $database->repository(User::class);

find()

public function find(mixed $id): ?object

Finds one entity by primary key.

$user = $users->find(1);

Returns null when no row is found.

findBy()

public function findBy(
    array $criteria = [],
    array $orderBy = [],
    ?int $limit = null,
    ?int $offset = null,
): array

Finds entities matching criteria.

$activeUsers = $users->findBy(
    ['active' => true],
    ['name' => 'ASC'],
    limit: 20,
);

firstBy()

public function firstBy(array $criteria = [], array $orderBy = []): ?object

Returns the first matching entity.

$ada = $users->firstBy(['email' => 'ada@example.com']);

This is equivalent to findBy($criteria, $orderBy, 1)[0] ?? null.

all()

public function all(): array

Returns all rows for the repository entity.

$allUsers = $users->all();

Use this only when the table is small or when you intentionally need all rows.

save()

public function save(object $entity): void

Persists an entity through the repository.

$users->save($user);

This delegates to Database::persist().

remove()

public function remove(object $entity): void

Deletes an entity through the repository.

$users->remove($user);

This delegates to Database::remove().

Clone this wiki locally