0

我正在尝试通过文本字段直接编辑相关数据对象的属性。例如,我有一个与每个页面相关的计数器对象。直接通过文本字段编辑计数器对象的值而不是转到相关对象并在那里编辑它会非常方便。那可能吗?

我至少可以像这样在文本字段中显示当前值:$fields->addFieldToTab('Root.Main', new TextField('ILikeCount.Count', 'ILikeCount', $this->ILikeCount()->Count), 'Content');

但保存新值不起作用。多谢,弗洛里安

4

1 回答 1

0

也许您可以在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();
}
于 2013-10-29T07:41:24.390 回答