silvershop/silverstripe-sqlquerylist
Composer 安装命令:
composer require silvershop/silverstripe-sqlquerylist
包简介
Wraps a SilverStripe SQLQuery in a SS_List
关键字:
README 文档
README
Create DB-driven SS_List instances from a SilverStripe SQLSelect query. Use them in templates and code like a DataList for display, iteration, counting, sorting, and limiting—without loading all rows into memory. Ideal for reporting and custom SQL-backed lists.
Installation
composer require silvershop/silverstripe-sqlquerylist
Why use it?
The SilverStripe ORM doesn’t let you treat arbitrary SQL as a list. Turning a SQLQuery result into an ArrayList forces loading every row into memory, which can be expensive or impossible for large result sets.
SQLQueryList wraps a SQLSelect and implements SS_List, so you get:
- Lazy execution – rows are only fetched when you iterate or call
toArray() - Efficient
count()– uses aCOUNTquery instead of loading all rows - Efficient
first()andfind()– useLIMIT 1under the hood - Sorting and limiting –
sort(),limit(), andwhere()modify the underlying query - Template-friendly – pass the list to templates and loop with
$List, paginate, etc.
Limitations: The list is read-only. You cannot add, remove, or mutate records. Methods like filter(), exclude(), map(), column(), and array access are not supported; build those constraints into your SQLSelect or use where() / sort() / limit() where available.
Basic usage
Build a SQLSelect and wrap it in SQLQueryList:
use SilverShop\SQLQueryList\SQLQueryList; use SilverStripe\ORM\Queries\SQLSelect; $query = SQLSelect::create( ['"Title"', '"ID"', '"Created"'], '"SiteTree"', ['"ParentID" = 0'] ); $list = SQLQueryList::create($query);
Use it like any SS_List:
// Count without loading all rows $total = $list->count(); // Get first record only (single row query) $first = $list->first(); // Iterate (executes query and yields each row as ArrayData) foreach ($list as $row) { echo $row->Title; } // Or get a plain array of ArrayData $rows = $list->toArray();
In a controller, pass it to the template:
public function getReportList() { $query = SQLSelect::create( ['"Name"', '"Total"', '"OrderCount"'], '"CustomerReport"', // e.g. a view or custom table ['"Total" > 100'] ); return new SQLQueryList($query); }
Template:
<% loop $ReportList %>
<p>$Name — $Total (orders: $OrderCount)</p>
<% end_loop %>
Custom row objects
By default each row is an ArrayData. To use your own objects (e.g. DTOs or custom models), set an output closure:
$list = new SQLQueryList($query); $list->setOutputClosure(function ($row) { return MyReportRow::create($row); });
Now first(), find(), toArray(), and iteration return instances of MyReportRow instead of ArrayData.
Sorting and limiting
sort(), limit(), and where() modify the underlying query and return the same list instance (fluent style):
$query = SQLSelect::create('*', '"Orders"'); $list = new SQLQueryList($query); // Sort by a column (ASC by default) $list->sort('"Created"'); // Or specify direction $list->sort('"Total"', 'DESC'); // Or multiple columns $list->sort(['"Status"' => 'ASC', '"Created"' => 'DESC']); // Limit and offset (e.g. pagination) $list->limit(10, 20); // 10 rows, skip first 20 // Add extra WHERE conditions $list->where('"Status" = \'Paid\'');
Then iterate or pass to templates as usual.
Finding a row by column value
Use find() to get one row by a column value (uses a single-row query):
$list = new SQLQueryList($query); $row = $list->find('ID', 123); if ($row) { return $row->Title; }
Debugging the query
To see the underlying SQL:
$sql = $list->sql();
API summary
| Method / behaviour | Supported |
|---|---|
count() |
Yes – uses SQL COUNT |
first() |
Yes – uses LIMIT 1 |
last() |
Yes – uses lastRow() query |
find($key, $value) |
Yes |
map($keyField, $titleField) |
Yes – returns a Map |
column($colName) |
Yes |
columnUnique($colName) |
Yes |
toArray() |
Yes |
getIterator() / foreach |
Yes |
sort(...) |
Yes |
limit($limit, $offset) |
Yes |
where($filter) |
Yes |
sql() |
Yes – returns SQL string |
setOutputClosure(Closure) |
Yes – custom row objects |
add(), remove() |
No – read-only list |
filter(), exclude() |
No – use where() or build into SQLSelect |
offsetGet / [] |
No |
Requirements
- PHP ^8.1
- SilverStripe Framework ^6
License
BSD-3-Clause
silvershop/silverstripe-sqlquerylist 适用场景与选型建议
silvershop/silverstripe-sqlquerylist 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41.22k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「query」 「list」 「silverstripe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 silvershop/silverstripe-sqlquerylist 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 silvershop/silverstripe-sqlquerylist 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 silvershop/silverstripe-sqlquerylist 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easily provide front-end sorting controls for SilverStripe lists
A block to display a list of links to child pages, or pages in current level
Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
Query filtering in your frontend
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 41.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 8
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-01-29