문제

I'm currently working on a project with CodeIgniter and its HMVC. http://jenssegers.be/projects/CodeIgniter-HMVC-Modules)

I have a general question regarding the best way to load a model in a different module. So far, I thought directly loading a model in a different module would be against MVC rule and accessed to a model indirectly by creating a function in the owner controller of the model.

For example,

class A
{
    function __construct()
    {
        ....
        $this->load->model('Model_A');
    }
    function getUsers()
    {
        $this->Model_A->getUsers();
        return $users;
    }
}

Class B
{
    ....
    function getModelAUsers()
    {
        $users_from_A = Modules::run('A/getUsers');  // This is currently how I do

        $this->load->model('A/Model_A'); // This is probably how I could do.
        $users_from_A = $this->Model_A->getUsers(); 
    }
}

To be honest, creating separate functions to access a model like this is pain in the ass... and the more I code, the harder I find it to refactor. Which way is a correct one for accessing a model in a different module in MVC style? or is there another best way to do it?

One more thing. This project will be heavily refactored and updated frequently.

도움이 되었습니까?

해결책

I've had the same issue, and finally decided to load a model within a model, when it is needed. This is not against MVC, because MVC is more about data flow, than system components including (loading). 'Normally' (without CI) you would use class autoloading (http://www.php.net/manual/en/language.oop5.autoload.php), while Codeigniter doesn't support this loading models within models is not a sin.

It is also good to notice, that even if you'll load some model more than once, Codeigniter will just ignore it. So even if you load some model twice, nothing bad will happen.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top