Welcome Guest, Not a member yet? Register   Sign In
DataMapper (OverZealous Edition) + HMVC
#1

[eluser]tarciozemel[/eluser]
Hi, folks!

I'm starting to develop a project with CodeIgniter and I decided to use DataMapper (OverZealous Edition), of course. Because the project have a considerable size, I'm also using HMVC.

During the development, I came across with a strange location error. I tried instance a new object of a datamapper model, but an error about location was appearing... With some researching, I found the solution and I want to share with you.

In first place, when we work with HMVC, it's possible to tell to DataMapper the location of the modules, as we can see in the Add Model Path section of the DataMapper user guide. So, we need to indicate the name of modules to DataMapper model autoloader.

To facilitate, I suggest you have a MY_Controller and use the snippet in the constuctor method, like this:

Code:
class MY_Controller extends CI_Controller {

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        
        Datamapper::add_model_path(
            array(
                APPPATH . 'modules/module1',
                APPPATH . 'modules/module2',
                APPPATH . 'modules/module3',
                APPPATH . 'modules/moduleN'
            )
        );
    }

}

OK, but when we do this, we can't access the add_model_path() because it's protected. So, as the WanWizard himself said, we need to alter 1 line of code of the application/libraries/datamapper.php file (in the actual version, the #822 line).

Change

Code:
protected static function add_model_path($paths)

For this:

Code:
public static function add_model_path($paths)

And that's it! Now the DataMapper it's able to see all your modules models! So, the steps are:

- Use the MY_Controller file (your controllers will have to extend it, of course)
- Configure the Datamapper::add_model_path array
- Change the #822 line in the application/libraries/datamapper.php file


I hope this mini tutorial helps someone!

Regards!
#2

[eluser]WanWizard[/eluser]
Datamapper also uses the HMVC module path to locate models.

Unfortunately, by default the path is only known from controllers inside the module. If you want to be able to load module models from outside the module, you can also use
Code:
$this->load->_add_module_path('module');
from HMVC's loader extension.




Theme © iAndrew 2016 - Forum software by © MyBB