也许您可以在onBeforeWrite()
钩子中捕获 textField 值
$fields->addFieldToTab('Root.Main', new TextField('ILikeCountCount', 'ILikeCount', $this->ILikeCount()->Count), 'Content');
textField 名称可以是任何名称,这里我刚刚删除了点符号以使事情变得更容易。然后捕获值onBeforeWrite()
并更新关系。
public function onBeforeWrite()
{
if( $this->ILikeCountCount )
{
// check if a $has_on realtion exist
if ( !$this->ILikeCountID )
{
// create new DataObject + relation
$ILikeCount = ILikeCount::create();
$ILikeCount->Count = $this->ILikeCountCount;
$ILikeCount->write();
$this->ILikeCountID = $ILikeCount->ID;
}
else{
// update existing relation
$ILikeCount = $this->ILikeCount();
$ILikeCount->Count = $this->ILikeCountCount;
$ILikeCount->write();
}
$this->ILikeCountCount = false; // clear to avoid duplicate writes
}
parent::onBeforeWrite();
}