I'm refactoring some code, and looking at ways to try and improve both readability and performance.
One item that bugs me is situations where I need a join statement with multiple objects on one side. For example...
Foo Schema Bar 2 Schema
-------------- ---------------
id id
data fooId
data
Result from Search:
---------------------
id barId fooData
1 1 ...
1 2 ...
1 3 ...
2 4 ...
3 5 ...
My end result, when querying for object Foo, needs to be an object Foo containing the id's (or objects fetched based on the id's) that are related.
Currently, I wind up having to condense multiple rows at the PHP level, adding bar id's to Foo until the foo id changes. It's a bit ugly, but it does work. What'd I'd like to reduce my result set to would be:
Result from Search:
---------------------
id barIds fooData
1 [1,2,3] ...
2 4 ...
3 5 ...
Is there any way to do this at the SQL level? (As a note, I'm not looking for the literal string 1, 2, 3, I want an array consisting of the id's 1, 2, and 3 -- but if I have to take a string and then transform, I can do)
As an aside, my intent is to combine this with the PDO::fetch_class to let me instantiate the class in a single line instead of spending time writing many lines of cookie-cutter code to load the properties of the class.