Welcome Guest, Not a member yet? Register   Sign In
Call to a member function on a non-object
#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.


Messages In This Thread
Call to a member function on a non-object - by El Forum - 01-13-2011, 09:10 AM
Call to a member function on a non-object - by El Forum - 01-13-2011, 09:22 AM
Call to a member function on a non-object - by El Forum - 01-13-2011, 11:37 AM
Call to a member function on a non-object - by El Forum - 01-13-2011, 11:38 AM
Call to a member function on a non-object - by El Forum - 01-13-2011, 11:39 AM
Call to a member function on a non-object - by El Forum - 01-13-2011, 12:12 PM
Call to a member function on a non-object - by El Forum - 01-13-2011, 12:21 PM
Call to a member function on a non-object - by El Forum - 01-13-2011, 12:50 PM
Call to a member function on a non-object - by El Forum - 01-13-2011, 02:15 PM
Call to a member function on a non-object - by El Forum - 01-13-2011, 02:30 PM
Call to a member function on a non-object - by El Forum - 01-13-2011, 02:54 PM
Call to a member function on a non-object - by El Forum - 01-13-2011, 02:55 PM
Call to a member function on a non-object - by El Forum - 05-26-2011, 12:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB