Content management system 的silverstripe edit属性通过textfield直接具有一个关系
我试图通过文本字段直接编辑相关数据对象的属性。例如,我有一个与每个页面相关的计数器对象。我可以非常方便地通过textfield直接编辑计数器对象的值,而不是转到相关对象并在那里编辑它。可能吗 我至少可以在文本字段中这样显示当前值:Content management system 的silverstripe edit属性通过textfield直接具有一个关系,content-management-system,silverstripe,Content Management System,Silverstripe,我试图通过文本字段直接编辑相关数据对象的属性。例如,我有一个与每个页面相关的计数器对象。我可以非常方便地通过textfield直接编辑计数器对象的值,而不是转到相关对象并在那里编辑它。可能吗 我至少可以在文本字段中这样显示当前值:$fields->addFieldToTab('Root.Main',new textfield('ILikeCount.Count','ILikeCount',$this->ILikeCount()->Count','Content') 但是保存新的值是行不通的。 许
$fields->addFieldToTab('Root.Main',new textfield('ILikeCount.Count','ILikeCount',$this->ILikeCount()->Count','Content')代码>
但是保存新的值是行不通的。
许多人,
florian也许您可以在onbeforewite()钩子中捕获textField值
$fields->addFieldToTab('Root.Main', new TextField('ILikeCountCount', 'ILikeCount', $this->ILikeCount()->Count), 'Content');
textField名称可以是任何名称,这里我刚刚删除了虚线符号以简化操作。然后在onbeforewite()
中捕获值并更新关系
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();
}
嘿那是个好主意!唯一的问题是访问textfield及其值$由于某种原因,->ILikeCountCount不存在。我还尝试将textfield存储在私有变量中。。。我可以在getCMSFields函数中回显它的值,但是在onbeforebroite函数中,textfield似乎不再可访问了。这很奇怪,我已经使用过很多次了,没有问题。你介意发布一个链接到你的getCMSFields()
?我发现的主要问题是,在你的代码中,我看不到你在哪里创建了$has\u one关系。因此,我更新了代码以创建ILikeCount
对象(如果它不存在)。这在我的设置中运行良好。酷,我刚刚粘贴了你的代码。那很好用。唯一奇怪的是:没有if语句if($this->ILikeCountCount)
它就不能工作。我想这是以前的问题。谢谢