我找到了一些过滤示例,但没有足够清晰的内容来回答我的问题。我有以下功能来获取我的孙子页面。我正在尝试对它们进行计数,但前提是它们满足某些条件。就我而言,如果它们没有 X、Y、Z,则将它们包含在计数中。
换句话说,想向函数添加一个参数列表/数组,如果 ANY 为 true,则不包含它们并将它们过滤掉。例如,如果 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;
}
请参阅 DataList::exclude
的 API 文档和 docs
如果您想排除数据库中是否有更多列为 true,则必须使用 :not
searchfilter 修饰符,不幸的是没有 excludeAny()
功能。
->filter(array('DealerOnly:not' => true))
->filter(array('Foo:not' => 'Bar'))
关于php - SilverStripe 3 过滤/过滤掉函数中的数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32099701/