Git Product home page Git Product logo

php-one-to-many-iterator's Introduction

One-to-many iterator

Helper iterator and generator for one-to-many joins.

Overview

When you want to fetch a one-to-many relation, you're probably using a JOIN to avoid the N+1 selects problem.

Though, it may be difficult to iterate over the result, especially when you need the whole "many" part of the relation loaded for a process.

This is why I created this tiny library. It takes a Traversable of arrays, having a common key to distinguish the "one" part of the one-to-many relation, and sorted on this key (so it can yield items in streaming, without loading the whole set in memory).

It will then aggregate the "many" part of the relation in a configurable key.

Installation

composer require val/one-to-many-iterator

Examples

Your database result iterator, once converted into an array, looks like this (a typical JOIN):

<?php

[
    ['id' => 1, 'parent_column' => 'hello', 'child_column' => 'foo'],
    ['id' => 1, 'parent_column' => 'hello', 'child_column' => 'bar'],
    ['id' => 2, 'parent_column' => 'world', 'child_column' => 'baz'],
];

But you'd like to iterate over something like this:

<?php

[
    [
        'id' => 1,
        'parent_column' => 'hello',
        'children' => [
            ['child_column' => 'foo'],
            ['child_column' => 'bar'],
        ],
    ],
    [
        'id' => 2,
        'parent_column' => 'world',
        'children' => [
            ['child_column' => 'baz'],
        ],
    ],
];

To achieve this, just pass your database result to Val\Iterator\OneToManyIterator or Val\Iterator\OneToManyGenerator, while configuring the common key (here, id), and aggregate key (here, children).

Assuming $result contains the raw SQL result iterator:

<?php

// With an iterator
$aggregated = new OneToManyIterator('id', 'children', $result);

// With a generator
$aggregated = new OneToManyGenerator('id', 'children', $result);

foreach ($aggregated as $i => $parent) {
    $parent['id'];
    $parent['parent_column'];

    foreach ($parent['children'] as $child) {
        $child['child_column'];
    }
}

The difference between the iterator and the generator, is, well.. that the former implements a raw PHP iterator while the latter uses a PHP generator (available since version 5.5).

Bugs

  • When using a LEFT JOIN instead of a JOIN, thus not guaranteeing the presence of at least one relation item, the aggregate field will still contain one item of null values.

php-one-to-many-iterator's People

Contributors

valeriangalliat avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.