在null上调用成员函数get()

时间:2016-11-29 19:56:04

标签: php codeigniter

我在Codeigniter中收到以下错误:

  

严重性:注意   消息:未定义属性:贷款:: $ loan_m

     

文件名:administrator / loans.php   行号:11

get()函数可以很好地处理页面和用户,但是它会导致贷款控制器抛出错误。一直无法找出原因。

  

回溯:

     

文件:C:\ Ampps \ www \ ncb.dev \ public_html \ application \ controllers \ administrator \ loans.php   行:11   功能:_error_handler

     

文件:C:\ Ampps \ www \ ncb.dev \ public_html \ index.php   行:315   功能:require_once

我的模特

<?php
class MY_Model extends CI_Model {

    protected $_table_name = '';
    protected $_primary_key = 'id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    public $rules = array();
    protected $_timestamps = FALSE;

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

    public function array_from_post($fields){
        $data = array();
        foreach ($fields as $field) {
            $data[$field] = $this->input->post($field);
        }
        return $data;
    }

    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';
        }

        $this->db->order_by($this->_order_by);
        return $this->db->get($this->_table_name)->$method();
    }

    public function get_by($where, $single = FALSE){
            $this->db->where($where);
            return $this->get(NULL, $single);
    }

    public function save($data, $id = NULL){

        // Set timestamps
        if ($this->_timestamps == TRUE) {
            $now = date('Y-m-d H:i:s');
            $id || $data['created'] = $now;
            $data['modified'] = $now;
        }

        // Insert
        if ($id === NULL) {
            !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
            $this->db->set($data);
            $this->db->insert($this->_table_name);
            $id = $this->db->insert_id();
        }
        // Update
        else {
            $filter = $this->_primary_filter;
            $id = $filter($id);
            $this->db->set($data);
            $this->db->where($this->_primary_key, $id);
            $this->db->update($this->_table_name);
        }

        return $id;
    }

    public function delete($id){
        $filter = $this->_primary_filter;
        $id = $filter($id);

        if (!$id) {
                return FALSE;
        }
        $this->db->where($this->_primary_key, $id);
        $this->db->limit(1);
        $this->db->delete($this->_table_name);
    }
}

贷款模式

<?php

class Loan_M extends MY_Model{
    protected $_table_name = 'loans';
    protected $_order_by = 'id';
    public $rules = array(
        'loantype' => array(
            'field' => 'loantype', 
            'label' => 'LoanType', 
            'rules' => 'trim|required|intval'
        ),
        'accounttype' => array(
            'field' => 'accpinttype', 
            'label' => 'AccountType', 
            'rules' => 'trim|required|intval'
        ),
        'nameofcreditunion' => array(
            'field' => 'nameofcreditunion', 
            'label' => 'NameOfCreditUnion', 
            'rules' => 'trim|required|intval'
        ),
        'membernumber' => array(
            'field' => 'nameofcreditunion', 
            'label' => 'NameOfCreditUnion', 
            'rules' => 'trim|required|intval'
        ),
        'title' => array(
            'field' => 'title', 
            'label' => 'Title', 
            'rules' => 'trim|required|max_length[100]|xss_clean'
        ), 
        'firstname' => array(
            'field' => 'firstname', 
            'label' => 'FirstName', 
            'rules' => 'trim|required|max_length[100]|xss_clean'
        ),
        'lastname' => array(
            'field' => 'lastname', 
            'label' => 'LastName', 
            'rules' => 'trim|required|max_length[100]|xss_clean'
        ),
        'email' => array(
            'field' => 'email', 
            'label' => 'Email',
            'rules' => 'trim|required|max_length[100]|xss_clean'
        ),
        'datesubmitted' => array(
            'field' => 'datesubmitted', 
            'label' => 'DateSubmitted', 
            'rules' => 'trim|required|xss_clean'
        ),
    );

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

    public function get_new (){
        $loan = new stdClass();
        $loan->loantype = '';
        $loan->accountype = '';
        $loan->title = '';
        $loan->firstname = '';
        $loan->lastname = '';
        $loan->email = '';
        $loan->nameofcreditunion = '';
        $loan->membernumber = '';
        return $loan;
    }
}

贷款控制人     

class Loans extends Admin_Controller{
    public function __construct() {
        parent::__construct();
    }

    public function index(){
        // Fetch all loans
        $this->data['loans'] = $this->loan_m->get();

        $this->data['page_title'] = 'Loans';
        $this->data['subview'] = 'administrator/loans/index';
        $this->load->view( 'administrator/body', $this->data );
    }
}

1 个答案:

答案 0 :(得分:1)

我能够通过在构造函数中添加以下内容来解决问题:

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

因此,构造函数看起来像这样:

public function __construct() {
    parent::__construct();
    $this->load->model('loan_m');  
}