SilverStripe 3.1.x - DataList唯一字段值过滤器/如何使用ORM计算分组列表中的项目?

时间:2015-09-03 11:50:22

标签: filter field silverstripe datalist

在SilverStripe中,可以获取一堆记录,如下所示:

$entryRecords = Entry::get()->sort('Email');

如果我想知道我有多少条目记录,我可以在DataList上调用count方法,如下所示:

$totalEntryRecords = $entryRecords->count();

如果我想从DataList使用PHP中的SilverStripe ORM查找这些记录中有多少个唯一的电子邮件地址而不编写循环而不依赖于运行新的自定义数据库查询,我该怎么做?

这是我迄今为止所尝试的内容,但它不起作用:

GroupedList::create($entryRecords)->groupBy('Email')->Count(); // does not work

我收到以下致命错误: PHP致命错误:在非对象上调用成员函数Count()

GroupedList API显示有一个Count方法。所以不确定为什么那不起作用。

3 个答案:

答案 0 :(得分:2)

确定这有效......

count(GroupedList::create($entryRecords)->groupBy('Email'));

答案 1 :(得分:1)

为什么不只是“从...中选择registry settings for changing the timeout distinct”?

e.g。

$uniqueEmailsCount =  Entry::get()->distinct()->setQueriedColumns(array('Email'))->count();

我想这更有效率。

答案 2 :(得分:0)

DataList中没有一个名为“distinct”的方法,

$counter = GroupedList::create(SomeDataObject::get())->GroupedBy('Title')->count();

是你最好的选择。

相关问题