【问题标题】:Silverstripe 3 filter ArrayListSilverstripe 3 过滤器 ArrayList
【发布时间】:2019-12-21 09:48:50
【问题描述】:

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

其中 getVideosfromCategories() 返回一个合并的 ArrayList

我需要类似的东西:

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

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

如何过滤我的 ArrayList?

【问题讨论】:

  • ArrayListDataList 都实现了SS_Filterable 接口。您应该能够以相同的方式过滤两者。确切的问题是什么?

标签: arraylist filter silverstripe


【解决方案1】:

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

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

(我为它开了一个公关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();

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 2018-03-02
    • 2012-02-27
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多