为每个 SiteTree 项目保存一个字段

问题描述 投票:0回答:1

我有一个扩展,可以设置这样的搜索字段:

 public function onBeforeWrite()
    {
            
        parent::onBeforeWrite();        
        $this->getOwner()->SearchContent = $this->collateSearchContent();
    }

除了仅在新页面或更新页面外,它工作正常。我想循环遍历 SiteTree 并保存每个的 SearchContent。

在我的任务中,我这样做:

 foreach (SiteTree::get() as $item) {
         $item->getOwner()->SearchContent = $item->getOwner()->collateSearchContent();
        $item->write();
      }

我收到错误:

 Uncaught BadMethodCallException: Object->__call(): the method 'collateSearchContent' does not exist on 'WAIRC\BlockPage'

所以,快到了。但它没有使用 SiteTree 对象来查找该方法。如何访问 sitetree 对象?

silverlight-4.0 silverstripe
1个回答
0
投票

$this->getOwner()
适用于第一种情况,因为
$this
Extension
的实例,并且
$this->getOwner()
返回
SiteTree
记录。

在第二种情况下,

$item
SiteTree
记录,因此不需要
getOwner()

更重要的是,

save()
不起作用,因为
SiteTree
上不存在该方法(请参阅API文档)。

foreach (SiteTree::get() as $item) {
    $item->SearchContent = $item->collateSearchContent();
    $item->write();
}
© www.soinside.com 2019 - 2024. All rights reserved.