PHPackages                             afilina/nestedsql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. afilina/nestedsql

ActiveLibrary[Database &amp; ORM](/categories/database)

afilina/nestedsql
=================

.

24191[1 issues](https://github.com/afilina/nestedsql/issues)PHP

Since Dec 17Pushed 10y ago2 watchersCompare

[ Source](https://github.com/afilina/nestedsql)[ Packagist](https://packagist.org/packages/afilina/nestedsql)[ RSS](/packages/afilina-nestedsql/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Nested SQL
==========

[](#nested-sql)

PHP &gt;=5.4

You can install this file via [Composer](https://getcomposer.org/doc/00-intro.md):

```
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.

This is the output that you should expect.

```
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
-----

[](#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.

```
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:

```
$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:

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

For any omitted class, the function will use stdClass.

Contributing
------------

[](#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.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/e6110c95b59d9ef3f8801ace23a7dcf4259fe5bcb44a90d0d64edb8b3b8e3adf?d=identicon)[afilina](/maintainers/afilina)

---

Top Contributors

[![afilina](https://avatars.githubusercontent.com/u/199835?v=4)](https://github.com/afilina "afilina (7 commits)")[![kirilogan](https://avatars.githubusercontent.com/u/15700576?v=4)](https://github.com/kirilogan "kirilogan (2 commits)")

### Embed Badge

![Health badge](/badges/afilina-nestedsql/health.svg)

```
[![Health](https://phpackages.com/badges/afilina-nestedsql/health.svg)](https://phpackages.com/packages/afilina-nestedsql)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
