Donate to protect them directly or help international organizations.
Converting a Joined Resultset Into a Hierarchy
July 2nd, 2015
I needed nested resultsets in some projects. I've seen a lot of StackOverflow chatter which essentially pointed people towards Doctrine or Propel ORMs, which was overkill when you didn't want mapping or even model classes.
I built a very small function that will transform joined results into a hierarchy. This avoids using heavy ORMs if you just need this small feature. It's 74 lines of code and really easy to use. Since it's open source, feel free to improve it.
You'd give it a statement like this:
SELECT album.id AS albums__id, photo.id AS albums__photos__id FROM album LEFT JOIN photo ON photo.album_id = album.id;
and it will produce something this:
stdClass Object ( [albums] => Array ( [1] => stdClass Object ( [id] => 1 [photos] => Array ( [1] => stdClass Object ( [id] => 1 ) ) ) ) )
Optionally, you can replace stdClass by your own classes with just a single parameter.
It's on my GitHub. All the documentation is there.
Previous: Naming functions consistently Next: Forcing Doctrine to Persist