Ramon Henry
Ramon Henry

Reputation: 49

Call to a member function get() on null

I am receiving the following error in Codeigniter:

Severity: Notice Message: Undefined property: Loans::$loan_m

Filename: administrator/loans.php Line Number: 11

The get() functions work fine with pages and users but it throws and error with the loans controller. Have not been able to figure out why.

Backtrace:

File: C:\Ampps\www\ncb.dev\public_html\application\controllers\administrator\loans.php Line: 11 Function: _error_handler

File: C:\Ampps\www\ncb.dev\public_html\index.php Line: 315 Function: require_once

MY Model

<?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);
    }
}

Loans Model

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

Loans Controller

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

Upvotes: 1

Views: 5594

Answers (1)

Ramon Henry
Ramon Henry

Reputation: 49

I was able to resolve the issue by adding the following to my constructor:

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

Therefore the contructor will look something like this:

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

Upvotes: 1

Related Questions