Skip to content

fix(laravel): accept null on an optional belongsTo - #8666

Open
devtechedge wants to merge 1 commit into
api-platform:5.0from
devtechedge:fix-laravel-nullable-belongsto
Open

devtechedge wants to merge 1 commit into
api-platform:5.0from
devtechedge:fix-laravel-nullable-belongsto

Conversation

@devtechedge

Copy link
Copy Markdown

Thanks for the report.

An optional BelongsTo whose foreign key is nullable should accept an explicit null, the same way Doctrine already does.

EloquentPropertyMetadataFactory wraps a nullable plain column with Type::nullable(), but a relation always became Type::object() and never looked at the foreign key.

I mark that relation nullable when the foreign key column is nullable, using the column name the relation already reports.

PersistProcessor skipped a null relation because isset() is false for null, so the null stayed as a raw attribute and the insert looked for a column named after the relation.

A present null on BelongsTo or MorphTo now calls dissociate() and unsets that attribute before save.

Omitting the key still leaves the stored relation alone.

I ran the filtered Issue8664Test on PHP 8.4, and POST and PATCH with the relation set to null no longer 500, with the foreign key null afterward.

Fixes #8664

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.

1 participant