title: Looking for advice/best practice on building objects from query results
body: <p>Im going to try my best to explain myself. Not 100% sure how to word this. I have data in multiple tables something like</p>
  
  <pre><code>tbl_rooms
  tbl_tables
  tbl_people_sitting_at_table
  </code></pre>
  
  <p>All these tables have there linking id's and what I want to do is build a "room" object and not query the db in the loops. Right now what i have is something like this
  Pseudocode</p>
  
  <pre><code>foreach ($rooms as $room) {
      $room-&gt;tables = get_all_the_table_by_room_id($room-&gt;room_id);
  
  .....
  foreach ($room-&gt;tables as $table) {
      $table-&gt;people_sitting_at_table = get_all_the_people_by_table_id($table-&gt;table_id);
  </code></pre>
  
  <p>What I have work but I don't like the amount of queries required to build the object. Heres is an example what I am looking to send back</p>
  
  <pre><code> stdClass Object
  (
  [id] =&gt; 15
  [room_name] =&gt; room_name
  [tables] =&gt; Array
      (
           [0] =&gt; stdClass Object
               (
                   [id] =&gt; 34
                   [table_name] =&gt; table_name
                   [people_sitting_at_table] =&gt; Array
                       (
                           [0] =&gt; stdClass Object
                               (
                                   [id] =&gt; 45
                                   [name] =&gt; name
  </code></pre>
  
  <p>What im having a hard time with is knowing how to organize all that if i was to get all the data up front.. If I get all the tables in the room and then all the people at all the tables how do i get the people nested under the right table efficiently
  Not sure if this enough info to get my point across, if not let me know
  Here is some SQL for the exaple</p>
  
  <pre><code>SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";  
  SET time_zone = "+00:00";  
  
  CREATE TABLE `tbl_people_sitting_at_table` (  
    `id` int(11) NOT NULL AUTO_INCREMENT,  
    `name` varchar(100) NOT NULL,  
    `table_id` int(11) NOT NULL,  
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
  
  INSERT INTO `tbl_people_sitting_at_table` (`id`, `name`, `table_id`) VALUES
  (1, 'jim', 1),
  (2, 'bob', 1);
  
  CREATE TABLE `tbl_rooms` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
  
  INSERT INTO `tbl_rooms` (`id`, `name`) VALUES
  (1, 'room one'),
  (2, 'room two');
  
  CREATE TABLE `tbl_tables` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `room_id` int(11) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
  
  INSERT INTO `tbl_tables` (`id`, `name`, `room_id`) VALUES
  (1, 'table one', 1),
  (2, 'table two', 1);
  </code></pre>