I am running SilverStripe 3.4
I cannot find any documentation on programatically saving many many relationships extra fields. The following code simply will not work:
foreach ($notifications as $notification) {
$status = $notification
->Members()
->filter([
"ID" => Member::currentUserID()
])
->first();
$data['Read'] = $status->Read; // whenever I call this code, $status->Read is ALWAYS 0
$status->Read = 1;
$status->write();
}
The ORM classes:
class Notification extends DataObject {
private static $belongs_many_many = [
"Members" => "Member"
];
}
class Member extends DataObject {
private static $many_many = array(
"Notifications" => "Notification"
);
private static $many_many_extraFields = array(
"Notifications" => array(
"Read" => "Boolean"
)
);
}
Poking around, I have seen that DataObject::getChangedFields
filters out my Read
field because it is not a "database field"
Note: I have overridden Notification::onBeforeWrite
but:
- I don't think this is called
I have this code at the start of it:
protected function onBeforeWrite() { parent::onBeforeWrite(); $changedFields = $this->getChangedFields(); if (isset($changedFields['Read']) && count($changedFields) == 1) { return; } }