Skip to content

Instantly share code, notes, and snippets.

@mogetutu
Created September 3, 2012 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogetutu/3608329 to your computer and use it in GitHub Desktop.
Save mogetutu/3608329 to your computer and use it in GitHub Desktop.
A simple concept for basic JOIN statement in MY_Model
public function __call($method, $parameters)
{
if (strpos($method, 'with_') === 0)
{
$table = substr($method, 5);
return $this->with($table, $parameters[0]);
}
throw new \Exception("Method [$method] is not defined on the Model class.");
}
public function with($table, $parameters)
{
$pk = $table . '.id';
$fk = $this->_table. '.'. singular($table) .'_id';
$this->db->join($table, $pk . '=' . $fk, 'left');
return $this;
}
// Example
$this->posts->with_photos()->get_all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment