SilverStripe 3 过滤/过滤掉函数中的数据对象

SilverStripe 3 Filtering / Filtering Out DataObjects in a Function

我找到了一些过滤示例,但还不足以回答我的问题。我有以下功能来获取我的孙子页面。我正在尝试对它们进行计数,但只有 if 它们符合特定条件。在我的例子中,如果他们没有 X、Y、Z 则将它们包括在计数中。

换句话说,想向函数添加一个参数列表/数组,如果任何一个为真,则不包括它们并将它们过滤掉。例如,如果 DealerOnly = true 忽略。

我考虑过在模板中执行此操作并使用 if / else,但计数不会像这样显示,所以我没有走那条路。

欢迎使用其他方法。

<% loop $GrandChildren %>$Count<% end_loop %>   

可能的帮助: http://www.silverstripe.org/community/forums/data-model-questions/show/23507

这里有文档:(虽然不是我需要的) https://docs.silverstripe.org/en/3.1/developer_guides/model/searchfilters/

我的功能,returns 我的孙子页面。

public function getGrandChildren() {
    $ids = Page::get()->filter(array('ParentID' => $this->ID))->getIDList();
    $grandChildren = Page::get()->filter(array(
        'ParentID' => $ids
    ));

    return $grandChildren;
}

在我计算所有页面的模板中

$GrandChildren.Count

您可以使用 ->exclude,所以对于您的 DataList...

$grandChildren->exclude(array(
  'DealerOnly' => true
));

嗯,你可以随心所欲地操纵你的 DataList,例如

public function getGrandChildren() {
    $ids = Page::get()->filter(array('ParentID' => $this->ID))->getIDList();
    $grandChildren = Page::get()
        ->filter(array(
            'ParentID' => $ids
        ))
        ->exclude(array('DealerOnly' => true));

    return $grandChildren;
}

请参阅 API 文档 DataList::exclude and docs

如果要排除数据库中的任何更多列是否为真,则必须使用 :not searchfilter 修饰符,因为遗憾的是没有 excludeAny() 函数。

->filter(array('DealerOnly:not' => true))
->filter(array('Foo:not' => 'Bar'))

出于某种原因,我没有找到其他答案。我发现您可以在模板中进行过滤。

$GrandChildren.Filter('DealerOnly','0').Count