[eluser]jwindhorst[/eluser]
I ran into a similar issue and found it annoying that I had to return data from the model back to the controller before I could use the data to instantiate my own libraries. My solution was to add an object funtion to my custom MY_Loader file inside the application/core directory. Here is the function:
Code:
public function object($objectName, $params = false)
{
// Grab the super object
$CI =& get_instance();
// Grab the custom class path
$customClassFile = getcwd() . $CI->config->item('custom_class_path') . ucfirst($objectName) . '.php';
// Require the class file once
require_once($customClassFile);
// Fix the capitalization, and instantiate the object;
$realClassName = ucfirst($objectName);
$obj = new $realClassName($params);
// return the instantiated object.
return $obj;
}
Essentially this simply overloads the Loader to allow you to return instantiated objects. It relies on an additional parameter being added to the config file which arguably could have been placed in a separate custom config but since it's the only parameter I needed I left it in the default.
Code:
/*
|--------------------------------------------------------------------------
| Custom Class Path
|--------------------------------------------------------------------------
|
| File path to where you would like to store your custom object classes
| WITH a trailing slash:
|
| /application/classes/
|
*/
$config['custom_class_path'] = '/application/classes/';
In this case I called the directory classes but clearly it's arbitrary.
Having this in place, inside of a model I can sa:
Code:
if($pQuery->num_rows() > 0)
{
foreach($pQuery->result() as $row)
{
$projects[] = $this->load->object('project', $row);
}
}
This allows me to return a list of instantiated objects from my models (in this case Projects.getProjects) to my controllers thus keeping the controllers cleaner and abstracting off the logic in a better way. IMHO of course.
Hope it helps.