Repository navigation
fix(values): save custom field values without per-field queries - #304
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
Every save of a model with N active custom fields ran 2N extra read queries. In the Relaticle app (Sentry RELATICLE-CRM-7E, 34 events) a PATCH on a task cost 34 queries with 4 fields and 64 with 10. The repeats were
select * from custom_field_values where entity_type=? and entity_id=? and (custom_field_id=? and tenant_id=?) and tenant_id=? limit 1andselect * from custom_fields where id=? and active=? and tenant_id=? order by sort_order asc limit 1.What changed
UsesCustomFields::saveCustomFields()now loads the active fields with their options once, loads the model's existing values for those fields in one query, and matches them in memory by field id (and tenant column when multi-tenancy is on). A missing row is built withcustomFieldValues()->make(). The loadedCustomFieldis set on the value'scustomFieldrelation, soCustomFieldValue::setValue()no longer re-queries it.saveCustomFieldValue()is split intocustomFieldValueKey()(the lookup key, unchanged logic) andwriteCustomFieldValue()(encryption cast,setValue, save, change tracking). The public method still doesfirstOrNewand still letssetValuelazy-load the field.Why this way
The batch path gets its fields from the active, tenant-scoped
customFields()query, so handing them to the value is safe. The single-field public method is also called byImporterBuilderand tests with a field that may have been deactivated since it was loaded. Pre-loading the relation there would skip the "field is inactive" RuntimeException thatCustomFieldValueOrphanedRowTestpins, so I left that path alone. My first attempt set the relation in the shared writer and failed those two tests.The fields are loaded
with('options'). Before this PR each value lazy-loaded its own field as a single model, so an app withModel::preventLazyLoading()could read$value->customField->optionsin a value observer. A field from a multi-row query is a strict model, and that read throwsLazyLoadingViolationException. Relaticle's activity log does exactly this, and the first commit broke 53 of its tests. Eager loading costs one query per save and keeps that contract.Behaviour to know about
wasChangedtracking and theupdated_atstamp are the same code paths. Model events fire the same way, since each value is still saved individually.customFieldrelation now arrives withoptionsloaded.Tests
tests/Feature/Models/UsesCustomFieldsTraitTest.php, newQuery Countgroup:savedlistener can read the value's field options. It fails on301b3b79with the lazy-load violation and passes on the base957508e9.Checked
On the base commit
957508e9the count test failed:Failed asserting that 21 is identical to 9.(select queries for 10 vs 4 fields).At
bcdc2bfa:UsesCustomFieldsTraitTest22 passed;pint --test,rector --dry-run,phpstan analyseclean;pest --type-coverage --min=100100%.Inside Relaticle (v3.18.4 installed, this trait copied over the vendor file, then restored):
Not checked
$tenantargument that differs from the scoped tenant is covered only by the existing suite, not a new test.php-code-stylingcheck fails for a reason outside this PR: the action'scomposer:latestimage no longer hasmbstring, so Composer resolves Pint v0.1.7 (the Nov 13 run got v1.32.3), which crashes onmb_strimwidth.pint --testwith the locked Pint passes locally.