CodeIgniter Forums
Only Load Model in construct ??? Why - 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: Only Load Model in construct ??? Why (/showthread.php?tid=17507)



Only Load Model in construct ??? Why - El Forum - 04-07-2009

[eluser]Snoob[/eluser]
I have the code:
Quote: function index()
{
$this->load->model('user_Model');
$this->user_Model->a();
}
The method a() is used to echo A; (because this only is example);
But it not working.
After I have the code:
Quote: function __construct()
{
parent::Controller();
$this->load->model('user_Model');
}

function index()
{

$this->user_Model->a();
}
And It've worked, why?


Only Load Model in construct ??? Why - El Forum - 04-07-2009

[eluser]n0xie[/eluser]
Can you show me your code? I just tried it and it works fine:

Code:
/* controllers/test.php */
class Test extends Controller {
    function Test() {
        parent::Controller();

    }

    function index(){
        $this->load->model('User_model');
        $this->User_model->a();
    }

Code:
/* models/user_model.php */
class User_model extends Model{

    function User_model(){
        parent::Model();
    }

    function a(){
        echo 'a';
    }
}



Only Load Model in construct ??? Why - El Forum - 04-07-2009

[eluser]Snoob[/eluser]
Oh, sorry.
Now, my problem is: I have the filename of Controller same as filename of Model ( user.php) and it not work. I change the filename of Model into user_model.php, so it work.Can one explain me why?


Only Load Model in construct ??? Why - El Forum - 04-07-2009

[eluser]n0xie[/eluser]
From the userguide...
Quote:Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

The file name will be a lower case version of your class name.



Only Load Model in construct ??? Why - El Forum - 04-07-2009

[eluser]Snoob[/eluser]
Thanks your very much!!