arrilot / collectors
此包已被废弃,不再维护。未建议替代包。
0.2.3
2017-12-07 22:00 UTC
Requires
- php: >=5.6.0
- illuminate/support: >=5.4
Requires (Dev)
- phpunit/phpunit: ~5
README
PHP Collectors (开发中)
介绍
Collectors扫描项目/集合中的指定字段以获取ids,并从数据库或其他存储中获取详细数据
安装
composer require arrilot/collectors
用法
首先,你需要创建自己的收集器类。
use Arrilot\Collectors\Collector; class FooCollector extends Collector { /** * Get data for given ids. * * @param array $ids * @return array */ public function getList(array $ids) { ... } }
示例
$elements = [ ['id' => 1, 'files' => 1], ['id' => 2, 'files' => [2, 1]], ]; $item = [ 'id' => 3, 'another_files' => 3 ]; $collector = new FooCollector(); $collector->scanCollection($elements, 'files'); $collector->scanItem($item, 'another_files'); // You can also pass several fields as array - $collector->scanItem($item, ['field_1', 'field_2']); $files = $collector->performQuery(); var_dump($files); // result /* array:2 [▼ 1 => array:3 [▼ "id" => 1 "name" => "avatar.png", "module" => "main", ] 2 => array:3 [▼ "id" => 2 "name" => "test.png", "module" => "main", ], 3 => array:3 [▼ "id" => 3 "name" => "test2.png", "module" => "main", ], ] */
如果你已经知道它们,可以手动添加额外的ids。
$files = $collector->addIds([626, 277, 23])->performQuery();
你可以这样将select
传递给getlist
$files = $collector->select(['id', 'name'])->performQuery(); // $this->select is ['id', 'name'] in `->getList()` and you can implement logic handling it.
额外的过滤器也是如此。
$collector->where(['active' => 1])->performQuery(); // $this->where is ['active' => 1]
你可以使用点表示法来定位字段,例如
$collector->fromItem($item, 'properties.files');