title: Custom object returned by join in Codeigniter Active record
body: <p>I have these two database tables:</p>
  
  <p><strong>locations</strong><br/>
  id<br/>
  name<br/></p>
  
  <p><strong>users</strong><br/>
  id<br/>
  location_id<br/>
  last_name<br/>
  first_name</p>
  
  <p>I also have <strong>User</strong> and <strong>Location</strong> class, they both extends <strong>Model</strong> and contain some custom methods. For example, there is a <strong>get_full_name()</strong> method in the <strong>User</strong> class.</p>
  
  <p>I am using the following codes to load the data,</p>
  
  <pre><code>$this-&gt;db-&gt;select('users.id, locations.name as location_name, users.last_name, users.first_name');
  $this-&gt;db-&gt;from('users');
  $this-&gt;db-&gt;join('locations', 'users.location_id = location.id');
  $query = $this-&gt;db-&gt;get();
  $users= $query-&gt;custom_result_object('User'); //now $users is an array of User object
  </code></pre>
  
  <p>The <strong>custom_result_object</strong> is a built-in but undocumented function in Codeigniter. It accepts a string of class name and will use it to create objects of that class.</p>
  
  <p>With the above codes, I can access the data like this,</p>
  
  <pre><code>foreach($users as $user)
  {
   echo $user-&gt;id;
   echo $user-&gt;get_full_name();
   echo $user-&gt;location_name;
  }
  </code></pre>
  
  <p>But as you can see, location_name is now a field of the <strong>User</strong> object, what I want is the <strong>User</strong> object has a field of <strong>Location</strong> object that allows me to use it like this,</p>
  
  <pre><code>foreach($users as $user)
  {
   echo $user-&gt;id;
   echo $user-&gt;get_full_name();
   echo $user-&gt;location-&gt;name; // Location is an object
  }
  </code></pre>
  
  <p>I don't want to use DataMapper ORM or any other ORMs, and would also like to avoid the N+1 query performance issue.</p>
  
  <p>How can I do this with minimal amount of codes?</p>
  
  <p>Note: I made up this example just for the demonstration purpose, in my real case, there are quite a lot of tables and classes (which has custom methods defined on them).</p>
  
  <p>Many thanks to you all.</p>