Silverstripe 3 filter ArrayList

问题描述 投票:0回答:1

我们如何在Silverstripe 3中过滤ArrayList?

其中getVideosfromCategories()返回合并的ArrayList

我需要类似的东西:

$this->getVideosfromCategories()->filter('ID:LessThan', $id)->sort(array('ID' => 'DESC'))->first()

这些过滤器(filter('ID:LessThan',$ id))仅适用于DataList吗?

我如何过滤我的ArrayList?

arraylist filter silverstripe
1个回答
1
投票

这些过滤器(filter('ID:LessThan',$ id))仅适用于DataList吗?

是的,没错,搜索过滤器修饰符仅适用于DataList实例。(https://docs.silverstripe.org/en/3/developer_guides/model/searchfilters/)有趣的是该文档没有提及,我认为应该对其进行更新。

(我为此打开了一个PR https://github.com/silverstripe/silverstripe-framework/pull/9363

但是您可以修改当前代码以改为使用ID数组进行过滤,如下所示:

$idsWeWant = [];
if ($id > 0) {
  $idsWeWant = range(0, $id - 1); // "$id - 1" because you had "LessThan" modifier.
}

$this->getVideosfromCategories()
  ->filter('ID', $idsWeWant)
  ->sort(array('ID' => 'DESC'))
  ->first();
© www.soinside.com 2019 - 2024. All rights reserved.