retrieving data from multiple classes
Posted: Thu Jul 03, 2008 10:21 pm
Hi,
My problem:
I have a user class, a group class and a contact class.
User can belong to multiple groups and have multiple contacts.
In my user class I have a function $user->getAll();
This function should return a nested array of users (from the database) along with their groups and contact data.
Rather than doing a big SQL statement and then looping through the results to make an array, I would like to have a function in the group class $group->getAllForUser($user_id) and likewise for the contact class.
But let's say I want to order the results by group or by first_name (which is stored in the contact class), how would I go about doing this? Because the data is coming from multiple queries, so it's not like I can just put an 'ORDER BY' at the end of a query.
Would sorting the nested array by php be a big overhead?
Is it better to represent rowsets as an array of objects? Or just a nested array?
I have struggled with this problem for quite a while and have never come up with a good solution.
Any help or info on best practices would be much appreciated.
Cheers, Ben.
My problem:
I have a user class, a group class and a contact class.
User can belong to multiple groups and have multiple contacts.
In my user class I have a function $user->getAll();
This function should return a nested array of users (from the database) along with their groups and contact data.
Rather than doing a big SQL statement and then looping through the results to make an array, I would like to have a function in the group class $group->getAllForUser($user_id) and likewise for the contact class.
But let's say I want to order the results by group or by first_name (which is stored in the contact class), how would I go about doing this? Because the data is coming from multiple queries, so it's not like I can just put an 'ORDER BY' at the end of a query.
Would sorting the nested array by php be a big overhead?
Is it better to represent rowsets as an array of objects? Or just a nested array?
I have struggled with this problem for quite a while and have never come up with a good solution.
Any help or info on best practices would be much appreciated.
Cheers, Ben.
