简体   繁体   中英

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

Fatal error: Call to a member function result() on a non-object in D:\\wamp\\www\\ocss\\application\\core\\My_Model.php on line 111

class Report extends MY_Controller{
    public function item_ladger()
    {
        $material = $this->Material_Model->get(); // when i call it here it works fine
        $inventory = $this->db->query("CALL inventory($id)")->result_array();
        $material = $this->Material_Model->get(); // when i call it here it Generate Fatal error: Call to a member function result() on a non-object in
    }
}

what's the reason behind?

EDIT

this is my material model it has table name and all table fields

class Material_Model extends MY_Model
{
    const DB_TABLE = 'material';
    const DB_TABLE_PK = 'material_id';

    public $material_id;
    public $material_name;
    public $size;
    public $rate;
}

this is my MY_Model it has table name and get method to get all result

class MY_Model extends CI_Model {
    const DB_TABLE = 'abstract';
    const DB_TABLE_PK = 'abstract';

    public function get($limit = 500, $offset = 0,$desc=true) {
    if ($limit) {
        if ($desc)
            $query = $this->db->order_by($this::DB_TABLE_PK, 'DESC')->get($this::DB_TABLE, $limit, $offset);
        else
            $query = $this->db->get($this::DB_TABLE, $limit, $offset);
    }
    else {
        $query = $this->db->get($this::DB_TABLE);
    }

    $ret_val = array();
    $class = get_class($this);

    foreach ($query->result() as $row) {
        $model = new $class;
        $model->populate($row);

        $ret_val[$row->{$this::DB_TABLE_PK}] = $model;
    }
    return $ret_val;
}

Finally i have solved my problem by simply calling mysql query instead store procedure

class Report extends MY_Controller{
    public function item_ladger()
    {
        $material = $this->Material_Model->get(); // when i call it here it works fine
        $inventory = $this->db->query("My Query To Database")->result_array();
        $material = $this->Material_Model->get(); // now when i call it here there is no error
    }
}

but i am confused my that error acure when calling store procedure instead query

class Report extends MY_Controller{
    public function item_ladger()
    {
        $this->load->model("Material_Model"); # loding Model
        $inventory = $this->Material_Model->get_data(); # caling Model to do my code

        if ($inventory['cup']) { # retrivig data from retund array
            echo "I never ate Cup Cakes";
        }

    }
}

In Model

public function get_data()
{
    $query = $this->db->get('cake'); # get data from table
    $result = $query->result_array(); # re-Assign as objective array
    return $result; # return data
}

Codeigniter SELECT

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