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

[eluser]reglarecneps[/eluser]
Hello,

I'm really stumped by this error code that keeps coming up.
Code:
Fatal error: Call to a member function add() on a non-object in /.../system/application/models/customer_model.php on line 37

I am calling the 'add' function of my address model, but for some reason the model is not loading correctly. As you can see in the 'get_all_details' function, I also use the address model's 'get' function and it works wonderfully that time.

Please help!!

customer_model.php
Code:
<?php
class Customer_model extends Model {

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

    function get_all_details($id) {
        $this->load->model('address_model');
        $this->load->model('location_model','location');
        $this->load->model('job_model','job');

        $table_info = $this->get_customer_table_info_for($id); //TODO:build get_customer_table_info_for($id)
        $address = $this->address_model->get($table_info[0]['address_id']); //TODO:build address model, and get($id)
        $locations = $this->location->minimal_get_all_for($id);
        $jobs = $this->job->minimal_get_all_for($id,'active');
        $details = array(
            'name' => $table_info[0]['name'],
            'id' => $table_info[0]['id'],
            'address' => $address[0],
            'locations' => $locations,
            'jobs' => $jobs
        );

        return $details;
    }

...

    function add_new_customer($data) {
        $this->load->database();
        $this->load->model('address_model');
        $address_id = $this->address_model->add(array($data['street1'],$data['street2'],$data['city'],$data['state'],$data['zip']));
        $this->db->query("INSERT INTO 'customers' ('name' ,'address_id') VALUES (?,?)",array($data['name'],$address_id));
    }

}
?>

address_model.php

Code:
<?php
class Address_model extends Model {

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

    function get($id) {
        $this->load->database();
        $table_data = $this->db->query('SELECT * FROM addresses WHERE id = '.$id)->result_array();
        return $table_data;
    }

    function add($data) {
        echo 'got here';
        $this->load->database();
        if ($this->db->query("INSERT INTO  'addresses' ('street1', 'street2' ,'city' ,'state' ,'zip') VALUES (?,?,?,?,?)",$data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }
}
?>
#2

[eluser]reglarecneps[/eluser]
updated
#3

[eluser]William Lopes[/eluser]
I can't find your function "add_address", even where it is calling it, where is it?

ps.: I'm brazilian.
#4

[eluser]reglarecneps[/eluser]
I apologize, I didn't update the error message to reflect the changes that I made to the code, it's the add function from my address model that's giving me the trouble. (I've updated the original as well)
#5

[eluser]William Lopes[/eluser]
Apparently there's nothing wrong.

Try putting the function call to "get" in place of the function "add". By doing this you already know if the error is in "address_model.php" or "customer_model.php.

Hugs.




Theme © iAndrew 2016 - Forum software by © MyBB