ninedoors
ninedoors

Reputation: 125

Codeigniter - Call to a member function on a non-object

I have been pulling my hair out for too long looking at this error. I am using Codeigniter 2 and have created MY_Controller class to load a few settings into my session data.

Which you can see below:

class MY_Controller extends CI_Controller {  
    protected $ajax = 0;

    public function __construct() {
        parent::__construct();            
        $this->load->model('settings_model');

        //is the session user agent set
        if ($this->session->userdata('browser') == false)
        {
            $this->load->library('user_agent');
            $this->session->set_userdata(array(
                'browser' => $this->agent->browser(),
                'browser_version' => $this->agent->version(),
                'is_mobile' => $this->agent->is_mobile() ? 1 : 0
            ));                                                          
        }

        //is the settings loaded
        if ($this->session->userdata('league_name') == false)
        {                                       
            $this->Settings_model->get_general_settings();
        }

        //get the menu if we need to
        //if ($this->session->userdata('menu') == false)
            //$this->Settings_model->get_menu_items();  

        //set the main part of the title
        $this->set_title($this->session->userdata('league_name'));

        //get that referring url
        $this->session->set_userdata('refered_from', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : base_url()); 

        //ajax request
        $this->ajax = isset($_GET['ajax']) ? 1 : 0;
    }
}

Where I am running into the problem is I keep getting this error:

Fatal error: Call to a member function get_general_settings() on a non-object in C:\xampp\htdocs\osmdev\application\controllers\welcome.php on line 12

Here is the Settings_model.php file that I am loading, yes it is in my application/models folder:

class Settings_model extends CI_Model 
{                        
    // Call the Model constructor
    function __construct() {         
        parent::__construct();
    }

    //get the general settings
    function get_general_settings() {
        $query = "SELECT setting_id, variable, value FROM settings WHERE non_session = 0";
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));    
    }

    //get all the settings we have
    function get_all_settings() {        
        $query = "SELECT setting_id, variable, value FROM settings";
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));
    }

    //get a specfic setting variable
    function get_specific_setting($var) {           
        $query = "SELECT setting_id, variable, value FROM settings WHERE variable = '".$var;
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));   
    }

    //get a specific type of setting
    function get_type_setting($type) {
        $query = "SELECT setting_id, variable, value FROM settings WHERE action = '".$type;
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));    
    }

    //get all the menu items
    function get_menu_items($type=0) {
        $query = "SELECT menu_id, title, menu_url, parent_id, level, function_used, perm_id, icon FROM menu WHERE active = 1 AND is_admin_menu = '".$type;
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)  
            $items[$row['menu_id']] = $row;  

        $this->session->set_userdata('menu', $items);
    }  
}

I am trying to call the get_general_settings function. Can anyone see what I am doing wrong?

Upvotes: 1

Views: 5666

Answers (2)

Kenzo
Kenzo

Reputation: 3633

This error can happen when your database user doesn't have select permission for the table you're trying to query. It doesn't make sense, but it happens from time to time.

Upvotes: 0

Spatial Pariah
Spatial Pariah

Reputation: 351

You could try to setup the model to store $row into an array and then return the array.

Your model:

function get_general_settings() {
    $rows = array();
    $query = "SELECT setting_id, variable, value FROM settings WHERE non_session = 0";
    $result = $this->db->query($query);

    foreach ($result->result_array() as $row)
        $variable = $row['variable'];
        $value = stripslashes($row['value']);
        $rows[] = $variable[$value];

    return $rows[]

Your controller:

//is the settings loaded
    if ($this->session->userdata('league_name') == false)
    {                                       
        //set userdata to returned $rows[]
        $this->session->set_userdata($this->Settings_model->get_general_settings());
    }
//echo last_query to test in mysql
$this->db->last_query();

See if that will help solve your problem. I would print_r($this->Settings_model->get_general_settings()); just to see if anything was placed in the array.

Then if nothing is there, echo the last_query to see what it's asking MySQL for and then run that returned query in mysql and see if you get at least one row.

Upvotes: 1

Related Questions