0

ページのテンプレートでデータを取得するために機能する汎用関数を取得したいと思います。プロパティが設定されていない場合は、親または親の親などから取得します。ジェネリックとは、db、HasOne、HasMany、ManyMany などの関係から独立していることを意味します。ManyMany に対してこれを持っているが、それがオブジェクト、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)
4

1 に答える 1

1

Silverstripe でこれを行う組み込みメソッドはありません。これを行うには、独自の関数を作成する必要があります。

以下は、パラメーターによってページの has_one、has_many、または many_many リソースを取得するか、リソースが見つかるまでサイト ツリーを上に移動するか、ルート ページに到達する例です。

function getComponentRecursive($componentName) {

    // has_one
    if ($this->has_one($componentName)) {
        $component = $this->getComponent($componentName);
        if (isset($component) && $component->ID)
        {
            return $component;
        }
    }

    // has_many
    if ($this->has_many($componentName)) {
        $components = $this->getComponents($componentName);
        if (isset($components) && $components->count())
        {
            return $components;
        }
    }

    // many_many
    if ($this->many_many($componentName)) {
        $components = $this->getManyManyComponents($componentName);
        if (isset($components) && $components->count())
        {
            return $components;
        }
    }

    if ($this->ParentID != 0)
    {   
        return $this->Parent()->getComponentRecursive($componentName);
    }

    return false;
}
于 2013-11-17T23:18:39.990 に答える