Error Fatal error: Call to a member function insert() on a non-object

Error Fatal error: Call to a member function insert() on a non-object

我在插入数据库后使用回调,将值传递给另一个 table,我认为我使用的方法很好。但是当它插入到数据库中的另一个 table 时,它给了我这个错误,可能是在我的模型中,或者控制器中的某些东西,我对此错误一无所知,有人可以帮助我吗? 在有人说要放 $this->load->database(); 之前它已经在我的自动加载配置文件中了:)

致命错误:调用非对象上的成员函数 insert()

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Finances::$inventory_model

Filename: controllers/finances.php

Line Number: 125

控制器:

public function inventory_management($post_array,$primary_key)
    {  
        $this->load->model('inventory_model');

        if($post_array['id_expense']!=null){
            $type = 'add';
            $id_exp_detail = $primary_key;

            $result = $this->inventory_model->insert([
                'id_exp_detail' => $id_exp_detail,
                'name' => $post_array['brand'],
                'quantity' => $post_array['item_quantity'],
                'lote' => $post_array['package_code'];
                'type' => $type,
                'date' => date(),
                'id_user' => $this->session->userdata('id_user'),
                'id_entity' => $this->session->userdata('id_entity')
            ]);
        }else if ($post_array['name']!=null){
            $type = 'sub';
            $id_treatment = $primary_key;

            $result = $this->inventory_model->insert([
                'id_treatment' => $id_treatment,
                'name' => $post_array['name'],
                'quantity' => $post_data['recomended_dose'],
                'lote' => $post_array['id_package'],
                'type' => $type,
                'date' => date(),
                'id_user' => $this->session->userdata('id_user'),
                'id_entity' => $this->session->userdata('id_entity')
            ]);
        }else{
            $type = 'sub';
            $id_fertilization = $primary_key;

            $result = $this->inventory_model->insert([
                'id_fertilization' => $id_fertilization,
                'name' => $post_data['name'],
                'quantity' => $post_data['quantity'],
                'lote' => $post_data['id_package'],
                'type' => $type,
                'date' => date(),
                'id_user' => $this->session->userdata('id_user'),
                'id_entity' => $this->session->userdata('id_entity')
            ]);
        }

        if($result){
            echo "validation Complete";
        }

    }

型号

class CRUD_model extends CI_Model
{

    protected $_table = null;
    protected $_primary_key = null;

    // ------------------------------------------------------------------------

    public function __construct()
    {
        parent::__construct();
    }


    public function insert($data)
    {

        $this->db->insert($this->_table, $data);
        // return $this->db->insert_id();
    }

型号:

class inventory_model extends CRUD_model
{
    protected $_table = 'inventory_management';
    protected $_primary_key = 'id';

    // ------------------------------------------------------------------------

    public function __construct()
    {
        parent::__construct();
    }

    // ------------------------------------------------------------------------

}

这里是你调用错误的模型文件

$this->load->model('inventory_model');

应该是

$this->load->model('crud_model');

并且你在函数中使用错误的方式传递数组

$param = array('id_exp_detail' => $id_exp_detail, 'name' => $post_array['brand'], 'quantity' => $post_array['item_quantity'], 'lote' => $post_array['package_code'], 'type' => $type, 'date' => date(), 'id_user' => $this->session->userdata('id_user'), 'id_entity' => $this->session->userdata('id_entity')); 

$result = $this->inventory_model->insert($param);