CodeIgniter Forums
Loading model function - 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: Loading model function (/showthread.php?tid=30860)



Loading model function - El Forum - 05-28-2010

[eluser]DaTran[/eluser]
What is the best method if i want to repetitively use a function('say ex. retrieving a name based on id') within a model?

I know in know you can use $this->function() inside another but say i want to use this particularly repetitive function within all my models? or at least most of them.


Loading model function - El Forum - 05-28-2010

[eluser]Clooner[/eluser]
Extend the model to MY_Model and include the function you need in the MY_Model and it will be available in all the models. How to extend the model is documented in the manual.


Loading model function - El Forum - 05-28-2010

[eluser]DaTran[/eluser]
I created My_Model.php in the library's folder but it's giving an error on line 3

Code:
Fatal error: Class 'CI_Model' not found in ......./application/libraries/MY_Model.php on line 3

Code:
<?php

class My_Model extends CI_Model
{
    function eatmyfood()
    {
        echo 'EAT MY FOOD';
    }
}



Loading model function - El Forum - 05-28-2010

[eluser]Clooner[/eluser]
Dude! You give up too fast. Try
Code:
<?php

class My_Model extends Model
{
    function eatmyfood()
    {
        echo 'EAT MY FOOD';
    }
}



Loading model function - El Forum - 05-28-2010

[eluser]DaTran[/eluser]
How do I use the eatmyfood() function?

Im trying to echo $this->eatmyfood(); but it gives me undefined error. Sorry for giving up so easy if it seems that way Im new at this.