Skip to content

feat(ORM): allow repositories to run custom select queries - #66635

Open
susnux wants to merge 3 commits into
masterfrom
feat/orm-relation-criteria
Open

susnux wants to merge 3 commits into
masterfrom
feat/orm-relation-criteria

Conversation

@susnux

@susnux susnux commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Two gaps in OCP\AppFramework\ORM\Repository block apps from moving mappers with relations
or non-equality queries to the ORM.

Relations as criteria

The find and delete methods only accepted mapped columns as criteria. Passing a relation
property, e.g. findBy(['merchant' => $merchant]) for a ManyToOne, failed with a TypeError.

The owning side of a relation (ManyToOne, or OneToOne with a JoinColumn) now matches on
its join column in findBy(), findOneBy(), findByAfterId(), findByBeforeId() and deleteBy().
It accepts the related entity, its primary key, null, or a list of entities / primary keys:

$orders = $orderRepository->findBy(['merchant' => $merchant]);
$orderRepository->deleteBy(['merchant' => [$merchant, $otherMerchantId]]);

Unsupported properties (unknown, or the inverse mappedBy side), unsaved related entities and
entities of the wrong class now throw an InvalidArgumentException with a clear message.

Custom select queries

The hydration of query results was private, so a repository could not run e.g. a date range
query and still get entities with their relations resolved. New protected methods:

  • getSelectQueryBuilder(array $criteria = [], array $orderBy = []): IQueryBuilder returns the
    select query with all relations joined, filtered and sorted like findBy(). The entity's table
    is aliased as e.

  • findEntities(IQueryBuilder $query): \Generator runs it and hydrates all results.

  • findEntity(IQueryBuilder $query): object runs it and hydrates the single result, throwing
    DoesNotExistException / MultipleObjectsReturnedException.

    $qb = $this->getSelectQueryBuilder(['merchant' => $merchant], ['createdAt' => \SortDirection::Ascending]);
    $qb->andWhere($qb->expr()->gte('e.created_at', $qb->createNamedParameter($since, IQueryBuilder::PARAM_DATETIME_IMMUTABLE)));
    return iterator_to_array($this->findEntities($qb), false);

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

`IDBConnection::dropTable()` already adds the table prefix, so passing
the prefixed name made it drop a non-existing table and left the test
tables in place. Running the repository tests a second time against the
same database then failed because the tables already existed.

Assisted-by: ClaudeCode:claude-opus-5-5
The find and delete methods of `Repository` only accepted mapped columns
as criteria. Using a relation property, e.g. `findBy(['merchant' => $m])`
for a ManyToOne relation, failed with a TypeError, so there was no way to
query entities by their related entity.

The owning side of a relation (ManyToOne, or OneToOne with a JoinColumn)
now matches on its join column and accepts the related entity, its
primary key, null or a list of those. Properties that can not be used as
criteria now throw an `InvalidArgumentException` with a helpful message.

Assisted-by: ClaudeCode:claude-opus-5-5
The find methods only support equality conditions, and the hydration of
the results is private. So repositories had no way to run queries like
range conditions on a date column and still get hydrated entities with
their relations resolved, which blocks moving such mappers to the ORM.

Add three protected methods: `getSelectQueryBuilder()` returns the
select query with the relations joined, filtered and sorted like
`findBy()`, so it can be extended with custom conditions. `findEntities()`
and `findEntity()` run such a query and hydrate the result.

Assisted-by: ClaudeCode:claude-opus-5-5
@susnux susnux added this to the Nextcloud 36 milestone Nov 16, 2026
@susnux susnux added enhancement 3. to review Waiting for reviews labels Nov 16, 2026
@susnux
susnux marked this pull request as ready for review November 16, 2026 12:43
@susnux
susnux requested a review from a team as a code owner November 16, 2026 12:43
@susnux
susnux requested review from CarlSchwan, come-nc, leftybournes and salmart-dev and removed request for a team November 16, 2026 12:43

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants