Welcome Guest, Not a member yet? Register   Sign In
Loading Models in Models
#1

[eluser]axiom82[/eluser]
Developers debate that MVC does/doesn't allow loading of models in models. I have run into technical situations where loading one model into another is necessary. Such as in the case were one model needs to query for data that another model has already "clearly" standardized in an API. Not only does loading one model into another keep coding lean, recoding identical functions or identical queries for two models is absurd.

You want models in models, it makes sense. Here you go. Place this at the end of /system/libraries/Model.php::model(). Now you can use $this->load->model() inside of models.


Code:
if (floor(phpversion()) >= 5)
        {
            $this->load =& load_class('Loader');
            $this->load->_ci_autoloader();
        }
        else
        {
            $this->_ci_autoloader();
            
            // sync up the objects since PHP4 was working from a copy
            foreach (array_keys(get_object_vars($this)) as $attribute)
            {
                if (is_object($this->$attribute))
                {
                    $this->load->$attribute =& $this->$attribute;
                }
            }
        }
#2

[eluser]bubbafoley[/eluser]
you can already load models from other models:

Code:
<?php

class Foo_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->model('bar');
    }

    function get_bar()
    {
        return $this->bar->baz();
    }

}
#3

[eluser]InsiteFX[/eluser]
And besides you should not hack the Model core class file!

Extended it with a MY_Model

InsiteFX
#4

[eluser]axiom82[/eluser]
I stand corrected on both accounts. You can load models into models and you should extend the core with MY_Model. Thanks guys!




Theme © iAndrew 2016 - Forum software by © MyBB