Message of the day:
Security release 4.4.4 is out. Changelog: https://github.com/silverstripe/silverstripe-framework/blob/4/docs/en/04_Changelogs/4.4.4.md
SilverStripe 4 related information and questions.
I tend to go for the former, but if I needed to do something similar to the latter, I prefer use of the
setField() method. But you need to watch out that this also sets has_one's as well
The wierd thing with the latter is that they're not actually properties too, which can (but not always) cause e.g. phpstan in vscode to underline all the things
I lean towards the former too, to me it feels more elegant and somehow more SS "flavoured"
I tend to prefer the former for brevity in calls. But it depends on how much you're trying to update at once really.
In terms of strong preference over
->Field = ..., no I don't think there is one.
But this is certainly a gotcha I've run into before (a long time ago, but it's a lasting memory, which still holds true).
of course, this is a little nitpicky, because if you're creating a brand new object, then there's no ID which means
isInDatabase is false, meaning a write happens and folks are generally none the wiser.
the better equivalent for what you're trying to achieve with the first call is
MyDataObject::create()->update(['Field' => 'Value'])
the first is setting the initial state - e.g. when the object is created from the DB. If you then perform a write, nothing will happen (unless there is no ID).
The second explicitly updates an existing (in memory) DataObject, which flicks the
changed status. Writes will see there are changes to record, and so continues happily.