Skip to content

Collection: support filtering by non-main-side relationship without ->id#818

Merged
hrach merged 2 commits into
mainfrom
feature/filter-by-relationship-without-id
Jul 6, 2026
Merged

Collection: support filtering by non-main-side relationship without ->id#818
hrach merged 2 commits into
mainfrom
feature/filter-by-relationship-without-id

Conversation

@hrach

@hrach hrach commented Jul 6, 2026

Copy link
Copy Markdown
Member

Collection: support filtering by non-main-side relationship without ->id

When filtering by a relationship property that does not hold the FK column
(ONE_HAS_ONE non-main side, ONE_HAS_MANY, MANY_HAS_MANY), the ORM now
automatically joins the target table and compares against its primary key.

Previously, only the main side worked (e.g. m:1 or 1:1 isMain) because
the FK column lives in the current table. The non-main side would produce
invalid SQL referencing a non-existent column.

Example: findBy(['departure!=' => null]) on Alarm, where departure is a
non-main 1:1 relationship, now correctly emits a LEFT JOIN instead of
trying to use a non-existent column.

Closes #574

Collection: skip JOIN for main-side relationship->id

When the last token is the primary key of a main-side relationship
(MANY_HAS_ONE / ONE_HAS_ONE isMain), use the FK column directly instead
of joining the related table. E.g. author->id becomes author_id
without an unnecessary JOIN to the authors table.

When filtering by a relationship property that does not hold the FK column
(ONE_HAS_ONE non-main side, ONE_HAS_MANY, MANY_HAS_MANY), the ORM now
automatically joins the target table and compares against its primary key.

Previously, only the main side worked (e.g. m:1 or 1:1 isMain) because
the FK column lives in the current table. The non-main side would produce
invalid SQL referencing a non-existent column.

Example: findBy(['departure!=' => null]) on Alarm, where departure is a
non-main 1:1 relationship, now correctly emits a LEFT JOIN instead of
trying to use a non-existent column.

Closes #574
When the last token is the primary key of a main-side relationship
(MANY_HAS_ONE / ONE_HAS_ONE isMain), use the FK column directly instead
of joining the related table. E.g. `author->id` becomes `author_id`
without an unnecessary JOIN to the authors table.
@hrach

hrach commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

This change is part of the following stack:

Change managed by git-spice.

@hrach hrach merged commit ffb1fab into main Jul 6, 2026
12 checks passed
@hrach hrach deleted the feature/filter-by-relationship-without-id branch July 6, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Condition over table (without ->id) is not working

2 participants