我发现了一些过滤的例子,但没有足够清楚地回答我的问题。我有以下函数来获取我的孙子页面。我试图计数它们,但只有,如果它们符合某些标准。在我的例子中,如果不包含X,Y,Z,则将它们包含在计数中。
换句话说,想要向函数添加一个参数列表/数组,如果ANY为真,则不包含它们并将它们过滤掉。例如,如果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/
返回子代页面的函数。
public function getGrandChildren() {
$ids = Page::get()->filter(array('ParentID' => $this->ID))->getIDList();
$grandChildren = Page::get()->filter(array(
'ParentID' => $ids
));
return $grandChildren;
}
在我的模板中计算所有的页面
$GrandChildren.Count
你可以随心所欲地操作你的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
和docs
如果您想要排除数据库中的任何更多列为真,则必须使用:not
搜索过滤器修饰符,因为不幸的是没有excludeAny()
功能。
->filter(array('DealerOnly:not' => true))
->filter(array('Foo:not' => 'Bar'))
由于某种原因,我没有得到其他答案。我发现你可以在模板中过滤。
$GrandChildren.Filter('DealerOnly','0').Count
您可以使用->exclude,所以对于您的DataList…
$grandChildren->exclude(array(
'DealerOnly' => true
));