Welcome Guest, Not a member yet? Register   Sign In
Basic Model Help
#1

[eluser]Unknown[/eluser]
Hi,

Can someone help me with Models. I can get controllers / views / helpers working easily but I am struggling with models. Here is my small test controllers;

Code:
class Test extends Controller{

  function __construct()
  {
    parent::Controller();
  }

  function index()
  {
    $this->load->model('testmodel');
    echo $this->test->function1();
    echo "aa";
  }
}

And my test model;

Code:
class Testmodel extends Model {

  function Testmodel()
  {
    // Call the Model constructor
    parent::Model();
  }

  function function1()
  {
    return "aaa";
  }

}

What I am expecting to be displayed is;

aaa
aa

Is that correct ? I cant seem to get it to work when it loads the model.
#2

[eluser]mddd[/eluser]
Yes what you expect is correct. But if you load a model, you have to use the entire name.
Code:
$this->load->model('testmodel');
$this->testmodel->function1();   // that's 'testmodel', not 'test'

If you want a different name or if you need multiple instances of the same model, you can specify the name:
Code:
$this->load->model('testmodel', 'test1');
$this->load->model('testmodel', 'test2');
$this->test1->function1();
$this->test2->function1();




Theme © iAndrew 2016 - Forum software by © MyBB