PHP code example of xp-forge / aggregate

1. Go to this page and download the library: Download xp-forge/aggregate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xp-forge / aggregate example snippets


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'],
//     ]
//   ],
// ]