CI3 - Cannot access a function from the model |
Hey team, am I missing something fundamental in my CI config?
I can load a model (it seems to be loaded when I print_r $this) but cannot access a function from inside it. The model is named Projects.php and is in the "models" folder. Projects.php only contains this: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); And lines 10 and 11 in the view I'm using are: Code: $this->load->model('Projects'); The rest of the file works fine, does everything it should. And this is the error that comes up: Code: A PHP Error was encountered Why is it undefined if the model is already loaded? Do I need to do something else to use a model in a view?
Yes, you need to assign it.
https://codeigniter.com/userguide3/gener...o-the-view Controller: Code: $this->load->model('Projects'); In your view: Code: $projects->get_last_ten_entries();
OK thanks I'll try that. Really appreciate it.
But that method for accessing a model from inside a view isn't outlined on the model page OR the one you linked. (05-03-2020, 06:20 AM)jreklund Wrote: Controller: I've added this in, it errors out on the controller line assigning data. Code: A PHP Error was encountered Lines 47-50 in that document are: Code: $this->load->model('Projects'); Same issue.
The model are an object (PHP class) and can be assign like any other data, but there aren't a specifically note about it no. The same process apply to strings, arrays etc as well. Everything you want to use in the view needs to be passed down.
This need to be set in the Controller, not the view I missed the capital P. It needs to be the same. Code: $this->load->model('Projects'); Or better yet lowercase: Code: $this->load->model('projects');
"The model are an object".
Ahh got it. So even though the model hasn't "done" anything yet, it's still loaded in as an object. That solved things for me entirely. Really appreciate your help and the extra info offered up. All self taught here so I run into a wall sometimes. I'll get to reading about objects. Thanks again |
Welcome Guest, Not a member yet? Register Sign In |