silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 820 forks source link

SearchableDropdownTrait::saveInto calls DataObject::write #11284

Closed johannesx75 closed 3 days ago

johannesx75 commented 1 week ago

Module version(s) affected

5.2.11

Description

In the SearchableDropdownTrait::saveInto method, if the field is a has_one relationship, it triggers $record->write() see: https://github.com/silverstripe/silverstripe-framework/blob/5/src/Forms/SearchableDropdownTrait.php#L400

For DataObjects with lots of has_one relations that means, that onBeforeWrite gets called over and over again.

FormField and any other instances of saveInto should not call write - that should be called by the form itself.

How to reproduce

Reproduction steps

  1. Create Model with multiple has_one relations
  2. Save DataObject in ModelAdmin

Possible Solution

Remove $record->write(); from SearchableDropdownTrait.

Additional Context

No response

Validations

PRs

GuySartorelli commented 1 week ago

Hi there, Thanks for raising this. If you'd like to create a pull request to fix it, that would go a long way towards getting this resolved.

emteknetnz commented 1 week ago

Yeah it may be as simple as removing the call to $record->write() - I've had a quick look a some other implementations of FormField::saveInto() and by and large they do not call $record->write()

johannesx75 commented 1 week ago

I'll create a pull request for it. Problem is I don't understand enough about the SearchableDropdownTrait uses yet. So I can't say for sure what the consequences of that change would be or where to look for them.

GuySartorelli commented 3 days ago

PR merged. This will be automatically tagged by GitHub Actions