Created
September 3, 2012 10:12
-
-
Save mogetutu/3608329 to your computer and use it in GitHub Desktop.
A simple concept for basic JOIN statement in MY_Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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