【发布时间】:2016-07-20 23:29:34
【问题描述】:
我正在运行 SilverStripe 3.4
我找不到任何有关以编程方式保存许多关系额外字段的文档。下面的代码根本行不通:
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();
}
ORM 类:
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"
)
);
}
四处寻找,我看到DataObject::getChangedFields 过滤掉了我的Read 字段,因为它不是“数据库字段”
注意:我已经覆盖了Notification::onBeforeWrite 但是:
- 我不认为这叫
-
我在它的开头有这个代码:
protected function onBeforeWrite() { parent::onBeforeWrite(); $changedFields = $this->getChangedFields(); if (isset($changedFields['Read']) && count($changedFields) == 1) { return; } }
【问题讨论】:
-
您的意思是要覆盖 Member 类吗?它由框架提供,在这种情况下它可能具有优先权。为了争论起见,当您将 Member 的名称更改为 MyMember 时会发生什么?它会保存吗?
-
@elliot_at_silverstripe 我们正在使用 DataExtensions
标签: php silverstripe