我想获得一个通用函数,用于在页面模板中获取数据,如果未设置属性,则从父级或父级父级获取它等等。泛型是指独立于 db、HasOne、HasMany、ManyMany 等关系。假设我有这个用于 ManyMany,但想检测它是 Object、HasManyList 还是 ManyManyList 或一个值。有这样的内置功能吗?或者你会怎么做?
function ManyManyUpUntilHit($ComponentName){
$Component = $this->getManyManyComponents($ComponentName);
if($Component && $Component->exists())
return $Component;
$Parent = $this->Parent();
if(is_object($Parent) && $Parent->ID != 0){
return $Parent->ManyManyUpUntilHit($ComponentName);
} else {
return null;
}
}
在模板中:
$ManyManyUpUntilHit(Teaser)