Welcome Guest, Not a member yet? Register   Sign In
Call to a member function on a non-object
#1

[eluser]Muncken[/eluser]
I have searched around and found similar threads, but none that could solve my problem. Suddenly, like out of nowhere, I started getting the error Call to a member function ... on a non-object. As far as I am concerned I have changed nothing, but something must have gone wrong.

The error shows whenever I am trying to call a Model function from within a Controller. I load the model the appropriate place with the
Code:
$this->load->model('myModel')
feature and I get no errors whatsoever. But when trying to use the model function, like this:

Code:
$data = $this->myModel->getData();

... I get the error from above. The strange thing is that if I call the model function directly, using:

Code:
$data = myModel::getData()

... Then it works. I have not touched the code above, so the error must be somewhere else in my project.

I am ripping my hair off of frustration. Any help is appreciated.

Thanks.
Kasper
#2

[eluser]Cristian Gilè[/eluser]
The error is truncated. Please, post its entire message.

What is the exact name of your model? Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.

Code:
class User_model extends Model
this is valid for CI 1.7.3

Code:
class User_model extends CI_Model
this is valid for CI 2.0

The file name will be a lower case version of your class name:

Code:
application/models/user_model.php

Cristian Gilè
#3

[eluser]Muncken[/eluser]
It happens in all my files. I will post an example here:

class Groups extends Controller
{
......
.....
.....

function manage()
{
$this->load->model('groups_model');
if ($this->uri->segment(3) == "delete")
{
$this->groups_model->delete();
}
}
}

This gives me:

Fatal error: Call to a member function delete() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/cms/system/application/controllers/groups.php on line 16
#4

[eluser]Muncken[/eluser]
class Groups extends Controller
{
......
.....
.....

function manage()
{
$this->load->model('groups_model');
if ($this->uri->segment(3) == "delete")
{
$this->groups_model->delete();
}
}
}
#5

[eluser]Muncken[/eluser]
Sorry, forgot to code it - twice!

Code:
class Groups extends Controller
{
......
.....
.....
  
   function manage()
   {
      $this->load->model('groups_model');
      if ($this->uri->segment(3) == "delete")
      {
         $this->groups_model->delete();
      }
   }
}
#6

[eluser]Eric Barnes[/eluser]
You should post your model code. I agree with Cristian and it sounds like it is named wrong.
#7

[eluser]Muncken[/eluser]
Okay, here's a complete example. It's another model because it is smaller.

about.php

Code:
class About extends Controller
{
    private $path = 'secret/about/';
    private $data = array(
                            'sub_content' => 'edit',
                            'page_headline' => 'About',
                            'page_title' => 'About'
                        );
    
    function index()
    {
        Security::is_logged_in( $this->session->userdata('logged_in') );
        $this->load->helper('editor');
        
        $this->data['main_content'] = $this->path.'edit';
        $this->data['user'] = $this->session->userdata('username');    
        
        //load model
        $this->load->model('about_model');
        //get text from table about
        $this->data['rows'] = $this->about_model->get();    
                
        $this->load->view('includes/template', $this->data);    
    }
    
    function update()
    {
        //validate
        $this->form_validation->set_rules('text', 'About text', 'required');
        if ($this->form_validation->run() == FALSE) //validation not passed
        {
            $this->data['warning'] = warning(validation_errors()); //create warning with validation errors    
            
        }
        else //validation passed
        {
            $this->load->model('about_model'); //load model
            
            $data = array('text' => $this->input->post('text'),
                          'last_updated' => time()); //data to be passed
            
            $this->about_model->update($data); //update database
            
            $this->data['warning'] = warning("The text was succesfully updated.", 2); //tell user about succes
        }
        
        $this->index(); //show page again
    }
}

about_model.php

Code:
class About_model extends Model
{
    function get()
    {
        //rows: text
        $this->db->select("text");
        //table: about
        $this->db->from("about");
        //(field,value)
        //$this->db->where("id",2);
        
        $q = $this->db->get();
        
        if ($q->num_rows() > 0)
        {
            foreach ($q->result() as $row)
            {
                $data[] = $row;    
            }    
            return $data;
        }        
    }    
    
    function update($data)
    {
        //$this->db->update('about', $data);
        //$this->db->where();
        $this->db->update('about',$data);
    }
}

When loading the page, the following error shows:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: About::$about_model

Filename: controllers/about.php

Line Number: 23

Fatal error: Call to a member function get() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/cms/system/application/controllers/about.php on line 23


Both file names are spelled lower case. The Security::is_logged_in(....) is to prevent the exact same thing from happening. If I change this to $this->security->is_logged_in(...) (and the security library is autoloaded), I will get a similar error too.
#8

[eluser]Cristian Gilè[/eluser]
http://ellislab.com/forums/viewthread/174523/

Cristian Gilè
#9

[eluser]Muncken[/eluser]
Thanks for the naswer, Christian, but that thread didn't solve much for me. I have now tried to change default host from localhost to 127.0.0.1 and this has no effect at all. I am running XAMPP on Mac OS X Snow Leopard and CI v. 1.7.3.
#10

[eluser]Muncken[/eluser]
Hi again.

I have found the root of the problem. It is in my security library. The thing that causes the problem is that it extends the Controller class. Here's Security.php

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Security extends Controller
{
    function is_logged_in($logged_in)
    {
        $error = "<h1>Stop</h1><p>You do not have acces to view this page.<br /><a >Return</a></p>";
        
        if ($logged_in != md5(1))
        {
            die($error);
        }
        else
        {
            $segment = $this->uri->segment(1);
            $priv = $this->session->userdata("$segment");
            
            if($priv != 1)
            {
                die($error);
            }
        }
    }
}

If I remove the "extends Controller" part, I get rid of the error, but then I can't use $this-> to access uri- and session-library, which is neccessary for the library to work out. How can I handle this?




Theme © iAndrew 2016 - Forum software by © MyBB