https://github.com/xp-forge/aggregate
Aggregate data on lists
https://github.com/xp-forge/aggregate
aggregate n-plus-1 php7 php8 sql xp-framework
Last synced: 2 months ago
JSON representation
Aggregate data on lists
- Host: GitHub
- URL: https://github.com/xp-forge/aggregate
- Owner: xp-forge
- Created: 2019-08-11T13:19:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T10:19:09.000Z (over 1 year ago)
- Last Synced: 2025-03-12T23:02:14.805Z (7 months ago)
- Topics: aggregate, n-plus-1, php7, php8, sql, xp-framework
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Aggregate data on lists
=======================[](https://github.com/xp-forge/aggregate/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](https://packagist.org/packages/xp-forge/aggregate)Circumvents [`n + 1`-problems](https://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem-in-orm-object-relational-mapping) often occuring with SQL queries.
Example
-------
The following shows how the *Aggregate* class works, firing only **two** SQL queries instead of creating an `n + 1`-problem.```php
use util\data\Aggregate;
use rdbms\DriverManager;$conn= DriverManager::getConnection($dsn);
$posts= Aggregate::of($conn->query('select * from post'))
->collect('comments', ['id' => 'post_id'], function($ids) use($conn) {
return $conn->query('select * from comment where post_id in (%d)', $ids);
})
->all()
;// [
// [
// 'id' => 1,
// 'body' => 'The first post',
// 'comments' => [
// ['id' => 1, 'post_id' => 1, 'body' => 'Re #1: The first post'],
// ['id' => 2, 'post_id' => 1, 'body' => 'Re #2: The first post'],
// ]
// ],
// [
// 'id' => 2,
// 'body' => 'The second post',
// 'comments' => [
// ['id' => 3, 'post_id' => 2, 'body' => 'Re #1: The second post'],
// ]
// ],
// ]
```