Welcome Guest, Not a member yet? Register   Sign In
Not Finding My Model
#1

[eluser]hitraj47[/eluser]
CI doesn't seem to want to find my model in one of my controllers. I get this message:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Admin::$page_model
Filename: controllers/admin.php
Line Number: 19

Here is a partial version of controllers/admin.php (I have made a comment next to line 19):

Code:
<?php

class Admin extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->helper('form');
  $this->load->library('form_validation');
  $this->load->library('template');
  $this->load->model('page_model');
}

public function index() {
  $data['page_title'] = 'Admin Dashboard';
  $this->template->display_admin('admin',$data);
}

public function pages() {
  $data['pages_list'] = $this->page_model->get_page_list_admin(); // Line 19
  $data['page_title'] = 'Manage Pages';
  $this->template->display_admin('pages/list',$data);
}

And I have my model located here: models/page_model.php.

Here is a partial version of page_model.php:

Code:
<?php

class Page_model extends CI_Model {

public function __construct() {

  $this->load->database();
}

public function get_page_list_admin() {
  $this->db->select('page_id, page_url, page_title')->from('pages');
  $query = $this->db->get();
  return $query->result_array();
}

The thing is, I have another controller called 'Page' and that also uses the Page Model and that seems to work perfectly fine. I don't know why it won't work here. If you need any more code please let me know.
#2

[eluser]CI_expert_indian[/eluser]
provide the code for controller "page" please
#3

[eluser]hitraj47[/eluser]
Here is the code for controllers/page.php"

Code:
<?php

class Page extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->model('page_model');
  $this->load->library('template');
}

public function index() {
  $page = $this->page_model->get_page( $this->page_model->get_home_page() );

  if ($page === FALSE) {
   $this->error404();
  } else {
   $this->template->display($page);
  }
  
}

public function view($page_url = NULL) {
  $page = $this->page_model->get_page( $page_url );

  if ($page === FALSE) {
   $this->error404();
  } else {
   $this->template->display($page);
  }
}

public function error404() {
  $page['page_title'] = "Error 404: Page Not Found";
  $this->template->display($page,'error404');
}

}
#4

[eluser]CI_expert_indian[/eluser]
$data['pages_list'] = $this->page_model->get_page_list_admin(); ..... try to call another function and then check is really page_model not load here or is there any other problem in that function ..
#5

[eluser]hitraj47[/eluser]
[quote author="deep_sheera" date="1338531590"] $data['pages_list'] = $this->page_model->get_page_list_admin(); ..... try to call another function and then check is really page_model not load here or is there any other problem in that function ..[/quote]

I made a function called test() in my page_model.php:

Code:
public function test() {
$test = 'test';
return $test;
}

Then I assigned $data['pages_list'] to $this->page_model->test(); and I got the same error, this time PHP is saying Fatal error: Call to a member function test() on a non-object

Then when I changed it back to
Code:
$data['pages_list'] = $this->page_model->get_page_list_admin();
I get the same error as before, with PHP saying: Fatal error: Call to a member function get_page_list_admin() on a non-object

I even changed it to one of the functions that already existed in the page_model that works in my Page controller and I still get the same error.

EDIT:
Wow I just fixed it... In my admin.php controller I had:

Code:
public function __construct() {
  parent::__construct();
  $this->load->helper('form');
  $this->load->library('form_validation');
  $this->load->library('template');
$this->load->model('page_model');
}

As you can see I load the page_model last... I moved that up to the top so now it says:
Code:
public function __construct() {
  parent::__construct();
  $this->load->model('page_model'); // page model loads first
  $this->load->helper('form');
  $this->load->library('form_validation');
  $this->load->library('template');
}

This fixed the errors... I don't know why this worked, can someone explain it? Should models/helpers and libraries etc that are loaded in the constructor be loaded in a certain order?
#6

[eluser]CI_expert_indian[/eluser]
hitraj Smile really interesting .......




Theme © iAndrew 2016 - Forum software by © MyBB