Welcome Guest, Not a member yet? Register   Sign In
HMVC - How to load model from other module
#1

[eluser]yogal[/eluser]
Hi,

a really simple question here.

I am in
Code:
modules/guestbook/admin.php


and I would like to load model from a diffrient module
Code:
modules/admin/models/

This method:
Code:
$this->load->model('someModel', 'someModel');
is not working, because HMVC is looking only in the same module the controller is in, or in
Code:
application/models/


How can i do that with HMCV ? I tried using Modules::find, Modules::load Modules::load_file, but with no result. There has to be a simple way to do this.

Thanks in advance.
#2

[eluser]The Wizard[/eluser]
by module you mean a controller?
#3

[eluser]yogal[/eluser]
[quote author="herrkaleun" date="1233274712"]by module you mean a controller?[/quote]

Actually, no.

What I have:

admin module

Code:
application/
           modules/
                  admin/
                       controllers/
                       models/
                             someModel.php
                       views/

Guestbook module

Code:
application/
           modules/
                  guestbook/
                       controllers/
                             admin.php
                       models/
                       views/

I want to access someModel.php (admin module) from within admin.php controller (guestbook module).

Basically someModel is used to store pagination settings in database, and it is located in admin module because through this module I am assaigning settings, then from a whole diffrient module i want to access those settings, and initialize pagination with them ($this->pagination->initialize($config_from_db);

I hope this clears things up.

Regards,
yogal


EDIT:
I moved the model outside of admin module and placed it in models folder inside application. It might not be the best idea but at least every module using pagination has access to this model now and I am still following DRY.
Smile
#4

[eluser]dreamynsx[/eluser]
Yeah I was stuck on this also.

I ended up changing the controller code...so you can explicitly pass in the module_path.
I think what the code should do is maybe search in present controller, then if not found search in all available module paths for a matching model.

Code:
public function model($model, $object_name = NULL, $connect = FALSE, $module_path = null)
    {

        $ci = modules::$registry[$this->_class];

        ($_alias = $object_name) OR $_alias = strtolower(end(explode('/', $model)));
        
        
        if (isset(self::$models[$_alias]))
            return $ci->$_alias;

        $module_path = isset($module_path) ? $module_path : $this->_module;
        
        list($path, $model) = modules::find($model, $module_path, 'models/');
#5

[eluser]yogal[/eluser]
[quote author="dreamynsx" date="1233837743"]Yeah I was stuck on this also.

I think what the code should do is maybe search in present controller, then if not found search in all available module paths for a matching model.

[/quote]

Yea, thats how matchbox works but unfortunately not HMVC, therefore you cannot for example separate your controllers even further (no sub-folders in controller folder inside module).

And thanks for the code I will definitely try it out.
#6

[eluser]Unknown[/eluser]
Maybe for someone it could help.
Just use $this->load->model(module/model).
For more info, read this
https://bitbucket.org/wiredesignz/codeig.../wiki/Home




Theme © iAndrew 2016 - Forum software by © MyBB