Welcome Guest, Not a member yet? Register   Sign In
Help with Controller
#1

[eluser]Unknown[/eluser]
I just started using CI about weeks ago, and I'm trying to experiment what this framework can do by creating my own CMS-sort of portal.

I have a page called dashboard (editor.php). It is basically everything - events, uploads, posts, etc.
Now, I have a controller for dashboard which is admin.php

It looks like this:
Code:
class Admin extends CI_Controller
{
function __construct()
{
  parent::__construct();
  $this->is_logged_in();
}

function index()
{
  $this->load->view('login');
}

function dashboard()
{
  $this->load->model('gallery_model');
  $this->load->model('site_model');
  
  $data = array();
  
  $data['images'] = $this->gallery_model->get_images();
  $data['events'] = $this->site_model->get_events();

  $this->load->view('admin/editor', $data);
}
function is_logged_in()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  
  if (!isset($is_logged_in) || $is_logged_in != true)
  {
   echo '[removed]alert("You are not logged in.");[removed]';
   redirect('login', 'refresh');
  }
}
}

Now, basically my problem is at the 'dashboard' function. Because I need to call 2 or more models. Like you can see:

Code:
$this->load->model('gallery_model');
$this->load->model('site_model');

And of course, I need to call their methods.

Code:
$data['images'] = $this->gallery_model->get_images();
$data['events'] = $this->site_model->get_events();

But, it's giving me errors:

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

Fatal error: Call to a member function get_events() on a non-object in ...\application\controllers\admin.php on line 24

It's pertaining to the line where I am calling the get_events function.

I've checked my config files and see everything is loaded okay.

Code:
$autoload['libraries'] = array('database', 'session', 'xmlrpc', 'parser');

because I was expecting to show the events in the left sidebar and the latest uploads (of images) to be shown on the right part of the contents.

I already tried searching some solutions around the net, but didn't work for me. I also saw some same topics about my problem, but we have quite a different scenario.

Can anyone help me out!?
Thank you.
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Please make sure error reporting is fully enabled. Just put this at the top of your index.php file:
Code:
ini_set('display_errors', '1');
error_reporting(E_ALL);

Also, please post your Site_model model code. I'm currently trying to help someone with a similar issue, and I have to say, I'm a bit baffled.

Are you using a Mac by any chance?
What version of PHP are you using?
Did this ever work, or is it a problem that's only just cropped up?
#3

[eluser]Unknown[/eluser]
Thanks for the reply, TheFuzzy0ne.

Btw here's my code for site_model:

Code:
class Site_model extends CI_Model
{
function get_events()
{
  $q = "SELECT * FROM calendar";
  
  $query = $this->db->query($q);
  
  if ($query->num_rows() > 0)
  {
   foreach($query->result() as $row)
   {
    $data[] = $row;
   }
   return $data;
  }
}
}

I also put the error_reporting, and just shows the same error.

Fatal error: Call to a member function get_events() on a non-object in ...\Rustedsoul\application\controllers\admin.php on line 24

No, I am not using MAC. And my PHP version is: PHP Version 5.4.7

Well, at first I only call the first model "gallery model" and everything works fine. So I proceeded to the next model and load it in my function and also access the method just like the ones I did before.
After having these:

Code:
$this->load->model('gallery_model');
$this->load->model('site_model');
  
$data = array();
  
$data['images'] = $this->gallery_model->get_images();
$data['events'] = $this->site_model->get_events();

$this->load->view('admin/editor', $data);

The error appeared, and I'm stuck on this.

Thanks for your response, btw.
#4

[eluser]TheFuzzy0ne[/eluser]
I have a theory. I suspect that somewhere you're assigning a value to $this->site_model, and removing the model from it.

Please try the following as your controller constructor:
Code:
function __construct()
{
    parent::__construct();
    $this->load->model('site_model');
    die(print_r($this->site_model->get_events(), TRUE));
}

What I hope to see, is your model method working as expected. If it does, then your problem may have something to do with gallery_model.

Another thing to try would be to make a copy of site_model and rename it (site_model2 will do fine), and then load that and see if it works. It's a peculiar problem, but I suspect there's a simple solution.




Theme © iAndrew 2016 - Forum software by © MyBB