Skip to content

fix(database): auto resolve queries for new relations on insert and update#2058

Merged
brendt merged 9 commits into
tempestphp:3.xfrom
laylatichy:fix-relations-insert
Mar 21, 2026
Merged

fix(database): auto resolve queries for new relations on insert and update#2058
brendt merged 9 commits into
tempestphp:3.xfrom
laylatichy:fix-relations-insert

Conversation

@laylatichy

@laylatichy laylatichy commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

@brendt shipped a bug yesterday, didn't. notice queries handle inserting/updating relations separately

…anyThrough, and HasOneThrough relations

InsertQueryBuilder.resolveObjectData() crashes with ReflectionException when a model
has BelongsToMany, HasManyThrough, or HasOneThrough properties. These relations use
pivot/intermediate tables and should be skipped during insert, but the method falls
through to resolveRelationProperty() which calls asClass() on type 'array'.

Adds Tag, BookReview, and Reviewer fixture models and three failing test cases that
reproduce the bug.
…in InsertQueryBuilder

These relations use pivot/intermediate tables and have no column on the model itself.
Without this, resolveObjectData() falls through to resolveRelationProperty() which
calls asClass() on type 'array', throwing ReflectionException.
…ilder

When inserting a model with a BelongsToMany relation, related items with existing PKs
get pivot rows created automatically. New related items without PKs are inserted first,
then linked via pivot. Follows the same after-callback pattern as HasMany/HasOne.

Adds CreateTagTable and CreateBookTagTable migrations for testing.
…h in UpdateQueryBuilder

UpdateQueryBuilder had the same bug as InsertQueryBuilder — BelongsToMany/through
relations fell through to resolveRelationValue() which calls asClass() on array type.

BelongsToMany updates now delete existing pivot rows and re-insert (sync behavior).
HasManyThrough and HasOneThrough are skipped as they require intermediate models.
…th new relation types

Ensures BelongsToMany, HasManyThrough, and HasOneThrough relations on models don't
break Select (including with() eager loading), Count, or Delete query builders.

Also applies Mago formatting.
@github-actions

github-actions Bot commented Mar 20, 2026

Copy link
Copy Markdown

Benchmark Results

Comparison of fix-relations-insert against 3.x (f4a11f9b67ab9639a9ea679d493a03b0efbc4e5f).

Open to see the benchmark results

No benchmark changes above ±5%.

Generated by phpbench against commit 4e77736

@laylatichy laylatichy changed the title fix(database): auto resolve queries for belongs to many on insert and update fix(database): auto resolve queries for new relations on insert and update Mar 20, 2026
@laylatichy laylatichy marked this pull request as ready for review March 20, 2026 12:04
@brendt brendt merged commit 72afef3 into tempestphp:3.x Mar 21, 2026
77 checks passed
@brendt

brendt commented Mar 21, 2026

Copy link
Copy Markdown
Member

Thanks!

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.

2 participants