last day (14 days later) » 

11:00 AM
0
A: Codeigneter : How to call model method from controller class?

TigerYou are calling a model method without instance of Your model . In your below code it search that function inside your controller class not to model class. $this->update_last_active($this->session->userdata('user_id')); Here $this is an instance of your controller class not for Model class. ...

 
Of course, that makes sense. I did try $this->MY_Model->update_last_active($this->session->userdata‌​('user_id'));. Thanks!
 
@Jam3sn you are welcome. please accept the answer if it helps.
 
That said it now returns a Call to undefined method CI_Loader::update_last_active() error? See my updated question for the updated code
 
there was bug reported for file name convention for CI so might be you have to change your class name as My_model instaed of MY_Model. see here
 
Ok so if change the class to My_model and call $this->load->model('My_Model'); it returns the same error. If I also update the name of the file from MY_Controller to My_Controller it returns Class MY_Model not found.
 
11:00 AM
@Jam3sn in my given reference link it says, class file names now must be Ucfirst and everything else in lowercase. Read I wrote the class name My_model.
 
Hi Tiger
 
Hi
 
So ive renamed the class to My_Model
 
no it should be My_model
 
Right I see
 
11:02 AM
I have also updated my answer
 
Ok thanks, just tweaking my code now
 
Great, It works now ?
 
So i've changed the Class to My_model, i call My_model in the controller and the name of the file is My_model.php, now i get a runtime error Type: RuntimeException Message: Unable to locate the model you have specified: My_model
 
Might be you missed to change the file name as well
 
Not sure what you mean. I think its looking in the models folder rather than the core folder because if I copy My_model.php to the models folder its back to the Call to undefined method CI_Loader error
 
11:10 AM
yes it will look into model only
 
But because im extending CI_Model it should be in core
 
not for core file
you can extend it to some model for example
User_model extend My_model
than you need to load user_model in your controller
and than you can access that function using instance of user_model
 
But im extending CI_Model because I have methods in there that i want accessible in all models.
 
I hope you understood
 
11:13 AM
CI_model is core built file of codeigneter, which provides some support to each of your model class which extend it
whenever you are creating new core file it means you need some common functionality with your models
which extend your custom My_model including CI_Model
 
Yeah and so I extend CI_Model and then in my other models, for example Login_model i use class Login_model extends MY_model
 
exactly now you have to load the login_model instance in your controller
and using that instance you can access your function
 
Yes thats what I had before hand
 
So whats wrong now ?
I am not able to understand ?
 
so in Login_model i was using $this->update_last_active($user['id']); which worked, but not in MY_Controller
 
11:18 AM
in controller you have to do as below
$login_model = $this->load->model('Login_model');
$login_model-> update_last_active($this->session->userdata('user_id'));
 
Okay, so I need to load in another model to access it then? I can't just access its methods from MY_Controller like Login (controller) can access Login_model
 
you do not need to load My_model in your controller
only load Login_model
 
okay, so $login_model = $this->load->model('Login_model');
$login_model->update_last_active($this->session->userdata('user_id'));
Fatal error: Call to undefined method CI_Loader::update_last_active() in /home/codethat/public_html/jamestut/application/core/MY_Controller.php on line 26
 
Why you need to load model in you core file ?
I mean My_Controller
 
Because there's a method in the model that I want to access from MY_Controller
 
11:25 AM
You need to use autoloading than
I think Loader class wont work for core files
 
But MY_Model is a core file so it's loaded by default
 
0
Q: codeigniter load model in core folder

Roberto RizziI'm new with CodeIgniter and I'd like to load a model in core/MY_Lang.php class. class MY_Lang extends CI_Lang { /** array $languages Array of languages */ var $languages = array(''); /** array $special Special URIs (not localized) **/ var $special = array(''); /** string $...

 
Okay, but because it's in core the autoloader wont find it right?
I already autoload login too
Login_model that is
 
I think you are going in wrong direction, try to create some helper function might be that can help
 
But I use that method in other models
Let me explain a little more
 
11:29 AM
I hope this can help you better philsturgeon.uk/codeigniter/2010/02/08/…
 
I have Login_model which is autoloaded. Login_model extends MY_Model which extends CI_Model. This works. In Login_model I have access MY_Models methods, such as update_last_active();
 
I am sorry I have to leave now.
 
Ok thanks for your help
 
Just for reference MY_ is prefix from config.php file. It is NOT My_ it IS MY_.
Ucfirst is for naming standardization in models, controllers and libraries directories. Not for core directory.
 

last day (14 days later) »