Welcome Guest, Not a member yet? Register   Sign In
CI3 - Cannot access a function from the model
#1

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?
Reply
#2

(This post was last modified: 05-03-2020, 06:22 AM by jreklund.)

Yes, you need to assign it.

https://codeigniter.com/userguide3/gener...o-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();
Reply
#3

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.
Reply
#4

(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
Reply
#5

(This post was last modified: 05-03-2020, 07:25 AM by jreklund.)

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);
Reply
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB