CodeIgniter Forums
Basic Model Help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Basic Model Help (/showthread.php?tid=32738)



Basic Model Help - El Forum - 08-03-2010

[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.


Basic Model Help - El Forum - 08-03-2010

[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();