2017 © Pedro Peláez
 

library nestedsql

.

image

afilina/nestedsql

.

  • Thursday, December 17, 2015
  • by afilina
  • Repository
  • 4 Watchers
  • 26 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Nested SQL

PHP >=5.4, (*1)

You can install this file via Composer:, (*2)

composer require afilina/nestedsql dev-master

This is a simple function to fetch your PDO statement as a nested resultset. This is meant as an alternative for using ORMs when you're not interested in the rest of their features., (*3)

This is the output that you should expect., (*4)

stdClass Object
(
    [albums] => Array
        (
            [1] => stdClass Object
                (
                    [id] => 1
                    [photos] => stdClass Object
                        (
                            [id] => 1
                        )
                )
            [2] => stdClass Object
                (
                    [id] => 2
                    [photos] => stdClass Object
                        (
                            [id] => 3
                        )
                )
        )
)

Usage

Here's how you format your SQL. The function assumes that you're using an id alias for each object and that it's unique., (*5)

SELECT album.id AS albums__id, photo.id AS albums__photos__id
FROM album
LEFT JOIN photo ON photo.album_id = album.id;

To use the function, simply require it like this:, (*6)

$statement = $pdo->prepare($sql);
$statement->execute();
$fetch_nested_sql = require 'src/NestedSql.php';
$result = $fetch_nested_sql($statement);

If you'd like to use custom classes instead of stdClass, pass them in the second parameter. If you'd like to specify singleton properties, pass them in the third parameter:, (*7)

$result = $fetch_nested_sql($statement, [
    'albums' => 'CustomAlbum',
    'photos' => 'CustomPhoto',
], [
    'photos'
]);

For any omitted class, the function will use stdClass., (*8)

Contributing

This was quick and dirty way to solve a problem in my project. I am definitely open to pull requests if you find a better way to do things or add useful features. Feel free to incorporate it into your libraries as long as you keep the attribution., (*9)

The Versions

17/12 2015

dev-master

9999999-dev https://afilina.com/

.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4
  • ext-pdo *

 

by Anna Filina

orm sql pdo