Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Error on DB query: "Call to a member function result() on a non-object"
#1

[eluser]Byrro[/eluser]
Hello,

I´m starting with CI and just can´t make this work:

1) I have two models ('client' and 'company') that are called in the same controller ('manager').

Code:
// Model 'Client': (file: 'client.php' at models directory)

class Client extends Model {

      function Client() {
            parent::Model();
      }


      function get_num_companies($client_id) {
            
            $this->load->database('mysql://root:@localhost/clients');
            $this->db->select('company_code');
            $query = $this->db->get_where('clients_list', array('id' => $client_id), '1', '');
            
            return $query->result();
            
      }

}

///////////////////////////////////////////////////////////////////////////

Code:
// Model 'Company': (file: 'company.php' at models directory)

class Company extends Model {

    function Company() {
        parent::Model();
    }

    function get_company_data($company_code) {
            
        $this->load->database("mysql://root:@localhost/company_001");
        $this->db->select('company_name');
        $query = $this->db->get_where('company_data', array('code' => $company_code), '1', '');
        
        return $query->result();
        
    }

}

///////////////////////////////////////////////////////////////////////////

Code:
// Controller 'Manager': (file: 'manager.php' at controllers directory)

class Manager extends Controller {

    function Manager() {
        parent::Controller();
    }

    function Company() {
    
    $client_id = 'XX';
    
    $this->load->model('Client');
    $returned_company_code = get_num_companies($client_id);
    
    $company_code = $returned_company_code[0]->company_code;
    
    $this->load->model('Company');
    $returned_company_data = get_company_data($company_code);
    
    $data['company_name'] = $returned_company_data[0]->company_name;
    
    $this->load->view('company', $data);
    
    }

}

2) The first model ('client') connects to the database 'clients' and the second model connects to another database called 'company_001'.

3) The problem is: the connection to the first database works just fine (tested), but I can´t work with the second model. It keeps giving me this error:

Quote:Fatal error: Call to a member function result() on a non-object in (...)

I´ve created a third DB to test with the second model, I´ve tried to connect to both BDs from the same Model, and got the same error! Could you please help me find where´s my mistake? Why isn´t it connecting to two databases?
#2

[eluser]mddd[/eluser]
If you work with multiple databases, don't load them like you do now. Calling $this->load->database will create the $this->db object and that will be they way you call it. If you load multiple databases, you need to create different objects for them. See the manual page on connecting to a database and scroll down to 'connecting to multiple databases'.

Addition: I'm glad you got it working but check this out anyway. You'll run into trouble if you initizalize the database, and later call a query on the other one. Or you'll have to load the right database before every query, but that's very inefficient. Better do something like this:
Code:
class Company extends Model {

    var $company_db;

    function Company() {
        parent::Model();

        // load the database here
        $this->company_db = $this->load->database('...', true);
    }

    function get_company_data($company_code) {

        // use the database here
        $this->company_db->select('company_name');
        $query = $this->company_db->get_where('company_data', array('code' => $company_code), '1', '');
        
        return $query->result();
        
    }

}
#3

[eluser]Byrro[/eluser]
Hello mddd,

Thank you very much! The problem wasn´t with the underscore, but really with the connection to multiple databases. I considered it wasn´t necessary because I was working with different models. Your tip solved the trouble!

Thank u again!
#4

[eluser]Nairobi[/eluser]
Just wanted to say thanks - I am using CI2 with HMVC (first real CI project and with HMVC as well), and have several models being called from a controller. I was getting this exact issue - weird because the errors started showing up all of a sudden and I didn't make any db changes - and it appears that the $this-db call in each model was causing the error.

Changed the db loading method for each model to give a $this->events_db and $this->jobs_db etc model specific db reference, and the problem disappeared.




Theme © iAndrew 2016 - Forum software by © MyBB