Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 1 month 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 (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T10:19:09.000Z (8 months ago)
- Last Synced: 2024-09-28T20:40:46.538Z (about 2 months ago)
- Topics: aggregate, n-plus-1, php7, php8, sql, xp-framework
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
Aggregate data on lists
=======================[![Build status on GitHub](https://github.com/xp-forge/aggregate/workflows/Tests/badge.svg)](https://github.com/xp-forge/aggregate/actions)
[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)
[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)
[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)
[![Latest Stable Version](https://poser.pugx.org/xp-forge/aggregate/version.png)](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'],
// ]
// ],
// ]
```