CodeIgniter Forums
CI3 - Cannot access a function from the model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: CI3 - Cannot access a function from the model (/showthread.php?tid=76320)



CI3 - Cannot access a function from the model - timps - 05-03-2020

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');
class Projects extends CI_Model {

    public function get_last_ten_entries()
        {
            $testString = "Inside the model";
        return $testString;
        }
    
}

And lines 10 and 11 in the view I'm using are:
Code:
        $this->load->model('Projects');
    $this->projects->get_last_ten_entries();

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
Severity: Notice

Message: Undefined property: CI_Loader::$projects

Filename: views/body.php

Line Number: 11

Why is it undefined if the model is already loaded?
Do I need to do something else to use a model in a view?


RE: CI3 - Cannot access a function from the model - jreklund - 05-03-2020

Yes, you need to assign it.

https://codeigniter.com/userguide3/general/views.html#adding-dynamic-data-to-the-view


Controller:
Code:
$this->load->model('Projects');
$data = [];
$data['projects'] = $this->projects;
$this->load->view('view', $data);

In your view:

Code:
$projects->get_last_ten_entries();



RE: CI3 - Cannot access a function from the model - timps - 05-03-2020

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.


RE: CI3 - Cannot access a function from the model - timps - 05-03-2020

(05-03-2020, 06:20 AM)jreklund Wrote: Controller:
Code:
$this->load->model('Projects');
$data = [];
$data['projects'] = $this->projects;
$this->load->view('view', $data);

In your view:

Code:
$projects->get_last_ten_entries();


I've added this in, it errors out on the controller line assigning data.

Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: Welcome::$projects

Filename: controllers/Welcome.php

Line Number: 49

Lines 47-50 in that document are:

Code:
$this->load->model('Projects');
$data = [];
$data['projects'] = $this->projects;
$this->load->view('body', $data);

Same issue. Sad


RE: CI3 - Cannot access a function from the model - jreklund - 05-03-2020

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');
$data = [];
$data['projects'] = $this->Projects;
$this->load->view('body', $data);

Or better yet lowercase:

Code:
$this->load->model('projects');
$data = [];
$data['projects'] = $this->projects;
$this->load->view('body', $data);



RE: CI3 - Cannot access a function from the model - timps - 05-03-2020

"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 Smile