My goal is to calculate some computed fields when I save an entry, based on the current data. Some of the data is inside a matrix field Payments that contains multiple Payment records inside it. I made the Plugin listen to the BEFORE_SAVE_EVENT
event of the Entry class, and now I need to read all the current Payments including the ones inserted during this save and use their data to compute another field.
The way I access these Payments from the matrix field is like this:
$entry = $event->sender;
Craft::dump($entry->payments);
I expect an array or some Matrix class, but what I get is a craft\elements\db\MatrixBlockQuery
which only shows the original Payments, without the newly inserted ones before saving. From what I understand, this result is because these are obtained by making a SELECT
into the database, so the new ones are not persisted yet. Also, after I don't get the expected data, the new payments are not persisted to the DB either. How do I access the Payments that are on the model but not persisted yet?