SilverStripe 3 Filtering / Filtering Out DataObjects in a Function

断了今生、忘了曾经 提交于 2019-12-11 03:36:22

问题


I've found some examples of filtering but nothing clear enough to answer my question. I have the following function to get my grand children pages. I'm trying to count them but only if they meet certain criteria. In my case if they do not have X,Y,Z then include them in the count.

In other words would like to add a list / array of arguments to the function that if ANY are true then don't include them and filter them out. For example if DealerOnly = true ignore.

I thought about doing this in the template and using if / else but the count won't display like this so I haven't gone down that route.

Alternative methods welcome.

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

Possible help: http://www.silverstripe.org/community/forums/data-model-questions/show/23507

Docs here: (not quite what I need though) https://docs.silverstripe.org/en/3.1/developer_guides/model/searchfilters/

My function which returns my grandchild pages.

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

    return $grandChildren;
}

In my template which counts all the pages

$GrandChildren.Count

回答1:


Well, you can manipulate your DataList like you want, e.g.

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;
}

See API docs for DataList::exclude and docs

If you want to exclude if any of more columns in the database is true, you have to use the :not searchfilter modifier as unfortunately there is no excludeAny() function.

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



回答2:


For some reason I didn't get the other answers to work. I found out you can filter in the template.

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



回答3:


You can use ->exclude, so for you DataList...

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


来源:https://stackoverflow.com/questions/32099701/silverstripe-3-filtering-filtering-out-dataobjects-in-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!