简体   繁体   中英

Codeigniter App Returns `Fatal error: Call to a member function result() on a non-object in` on live but not local

I'm new to codeigniter and also to MVC approach to OOP so I am already resigned to the fact that this is a mistake I have made and not likely to be a issue outside of my own code.

I have a controller (Purchase_Order) which loads 2 Models (Product_m & Supplier_m) + others.

I have built a custom MY_Model which takes 0 params and generates a query based on the model settings (table_name, primary_key etc...).

In my local instance (Wamp) it works perfectly. As soon as I upload to the Live web server (Linux) I get the following error;

Fatal error: Call to a member function result() on a non-object in (MY_Model.php on line 58) .

I have another Controller (User) which loads the Model (User_m) and passes via the same MY_Model route and that works perfectly online and locally.

I can only assume I have a scope issue or something similar but I have spent the last 48hrs obsessing over fixes to no avail.

I really hope someone can spare the time to enlighten me on this subject.

MY_Model

class MY_Model extends CI_Model {    
//Vars
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $_rules = array();
//protected $_timestamps = FALSE;

//Constructor
function __construct() {
    parent::__construct();
}



// GET
public function get($id = NULL, $single = FALSE) {

    if($id != NULL){
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE){
        $method = 'row';
    }
    else{
        $method = 'result';
    }

    if(!count($this->db->ar_orderby)){
        $this->db->order_by($this->_order_by);
    }

    //echo var_dump($this);
    return $this->db->get($this->_table_name)->$method();
    //die('<pre>'.print_r($this->db, true).'</pre>');
}

Other Classes Removed as they were not part of the issue.

Many Thanks to all but ultimately @rojoca as the pertinent question: Looks like $this->db->get($this->_table_name) is returning null. Do you have $db['default']['db_debug'] set to FALSE? It's possible you have some database connection error. Looks like $this->db->get($this->_table_name) is returning null. Do you have $db['default']['db_debug'] set to FALSE? It's possible you have some database connection error. which led me to the answer;

I had previously exported the live DBase to a local instance to test which worked fine which naively led me to believe this was not where the problem was.

I was using a different database config setup for the live site which did have db_debug set to FALSE. Turning it back on quickly identified that the web server/service I am using had forced the table names to lowercase. I was completely unaware of this and I have never heard of this forcing approach before either.

Lesson learnt for me. Thanks again for all assistance on this issue. All credit should go to @rojoca for this answer. Thank you - time for sleep :)

When this happens, it means the codeigniter could not find a field in your database table (this has happened several times to me). The solution would theoretically create the fields that are missing.

To resolve this error, I use a small solution:

 <?php

   public function test($op)
    {
        $this->db->where('op', $op);
        $result = $this->db->get('test');
        return ($result instanceof CI_DB_mysql_result) ? $result->result() : false;
    }

Here we check if the variable is an instance of CI_DB_mysql_result. This error happens because the method does not return the instance of this class!!!

Sorry, I do not speak English. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM