Welcome Guest, Not a member yet? Register   Sign In
Loading a model within a model.
#1

[eluser]MPress[/eluser]
This doesn't seem to be working for me. $this->load->model('whatever'); will work but no $this->whatever reference is created.
#2

[eluser]MPress[/eluser]
Got it. You need something like this in a base model:
Code:
function load_model($model){
    $this->CI->load->model($model);
    $this->$model =& $this->CI->$model;
}
With $this->CI =& get_instance();
#3

[eluser]gtech[/eluser]
Not quite sure what your getting at, but I have managed to load a model within a model e.g.

Code:
class Usersdb extends Model
{
  /**
   * The Initialisation method
   */

  function Usersdb()
  {
    parent::Model();
    $this->load->model('loggingsdb');
    $this->load->model('resultsdb');
  }

  /**
   * Returns a users database row
   *
   * @param integer $id A User ID.
   * @return
   */
  function getUser($id)
  {
    $query = $this->loggingsdb->getwhere('users',array('UserID'=>$id));
    ...
#4

[eluser]MPress[/eluser]
That doesn't seem to work for me. I believe what's going on is that loading a model using $this->load->model is actually creating the original reference in the CI instance and these references are only copied over (localized) to the model upon construction. Therefore, any other model loaded post-construction of the subject model will only have a reference in the CI instance (controller) and not in the model itself. What could be happening in your case is that these models were already previously loaded and so the references have already been localized within the subject model (on construction).

My workaround fixes this problem, but if something else is going on here, perhaps it is unnecessary.
#5

[eluser]CodeOfficer[/eluser]
this works great for me:
Code:
class MyModel extends Model
{
    private $CI;
    
    function MyModel()
    {
        parent::Model();
        $this->CI =& get_instance();    
    }
    
    function whatever()
    {
        $this->CI->load->model('model1');    
        $this->CI->model1->method1();
    }
    
    function woot()
    {
        $this->CI->load->model('model2');    
        $this->CI->model2->method2();    
    }
}
#6

[eluser]gtech[/eluser]
Ok I get you, I only load my models within the model constructor and thats why it works in my case, if I moved the load outside the constructor it probably wont work.. Thanks for the info.
#7

[eluser]MPress[/eluser]
[quote author="gtech" date="1193848097"]Ok I get you, I only load my models within the model constructor and thats why it works in my case, if I moved the load outside the constructor it probably wont work.. Thanks for the info.[/quote]
No, I believe it works in your case because those models were already previously loaded in your CI instance (somewhere in your controller), before the construction of the subject model. At least this has been my experience.
#8

[eluser]MPress[/eluser]
[quote author="CodeOfficer" date="1193827453"]this works great for me:
Code:
class MyModel extends Model
{
    private $CI;
    
    function MyModel()
    {
        parent::Model();
        $this->CI =& get_instance();    
    }
    
    function whatever()
    {
        $this->CI->load->model('model1');    
        $this->CI->model1->method1();
    }
    
    function woot()
    {
        $this->CI->load->model('model2');    
        $this->CI->model2->method2();    
    }
}
[/quote]
So you manually code wrapper methods anytime you want to utilize a method of a secondary model within your subject model? Not sure if I'm a fan. With the above load_model function that I whipped up, you can simply do the following:
Code:
$this->load_model('model2');
$this->model2->whatever();
$this->model2->another_method();
$this->model2->yet_another();
#9

[eluser]gtech[/eluser]
Quote:
Quote: Ok I get you, I only load my models within the model constructor and thats why it works in my case, if I moved the load outside the constructor it probably wont work.. Thanks for the info.
No, I believe it works in your case because those models were already previously loaded in your CI instance (somewhere in your controller), before the construction of the subject model. At least this has been my experience.

Hello,

I wrote a little test that loads a model within a model, and works:

The controller mod.php
Code:
<?php
class Mod extends Controller {

  function Mod()
  {
    parent::Controller();
    $this->load->model('testmod');
  }
  function modtest() {
    $this->testmod->test();
  }
}
?>

then two models

testmod.php
Code:
<?php
class Testmod extends Model
{
  function Testmod()
  {
    parent::Model();
    $this->load->model('testmod2');
  }

  function test()
  {
    echo "hello1..";
    $this->testmod2->test();
  }
}
?>

testmod2.php
Code:
<?php
class Testmod2 extends Model
{
  function Testmod2()
  {
    parent::Model();
  }

  function test()
  {
    echo "hello2";
  }
}
?>

when I call

http://localhost/..../index.php/mod/modtest

it prints out:
Code:
hello1..hello2

Is this behavior you would expect?
#10

[eluser]mdg5w[/eluser]
That is the behavior I would like, however it is not working. And I'd like to avoid rewriting code.




Theme © iAndrew 2016 - Forum software by © MyBB