Skip to content

IgnitedRecord with HMVC

Derek Jones edited this page Jul 5, 2012 · 3 revisions

To use IgnitedRecord with Modular Extensions - HMVC you have to make two modifications to the Controller.php file and you cannot use the factory methods of IgnitedRecord (you must use model files).

The changes below is to version 5 of ME.

Add this method to the Controller class (which resides in libraries/Controller.php):

    /**
     * Loads the IgnitedRecord class and the Model class, also acts as a wrapper for Loader::model().
     * 
     * This enables an other way to instantiate IgnitedRecord's child classes:
     * @code
     * $this->load->ORM();
     * $this->load->model('a_child_class');
     * // or as a wrapper for model()
     * $this->load->ORM('a_child_class','pages');
     * @endcode
     * 
     * @param $model The model name of the model to load, if false ORM() will not load any derived classes (models)
     * @param $name The name parameter is passed to the model() function, determining if the property name should differ from the default
     * @param $db_conn The database connection passed to the model() function
     */
    function ORM($model = false, $name = FALSE, $db_conn = FALSE){
        if(!class_exists('IgnitedRecord'))
        {
            // change this line if the IgnitedRecord file is stored elsewhere
            require_once(APPPATH.'models/ignitedrecord/ignitedrecord.php');
        }
        
        if($model != false)
        {
            $this->model($model,$name,$db_conn);
        }
    }

In the model() method of the Controller class, you have to add this, right before the return statement:

        $CI =& get_instance();
        if( ! isset($CI->$_alias))
        {
            $CI->$_alias = $this->$_alias;
        }

The code below assigns the models correctly, so IgnitedRecord can find them.

Clone this wiki locally