Php 在null上调用成员函数get()
我在Codeigniter中收到以下错误: 严重性:通知 消息:未定义的属性:Loans::$loan\u m 文件名:administrator/loans.php 电话号码:11 get()函数可以很好地处理页面和用户,但它会抛出错误,并与贷款控制器一起出错。我还不知道为什么Php 在null上调用成员函数get(),php,codeigniter,Php,Codeigniter,我在Codeigniter中收到以下错误: 严重性:通知 消息:未定义的属性:Loans::$loan\u m 文件名:administrator/loans.php 电话号码:11 get()函数可以很好地处理页面和用户,但它会抛出错误,并与贷款控制器一起出错。我还不知道为什么 class Loans extends Admin_Controller{ public function __construct() { parent::__construct();
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 );
}
}
回溯:
文件:C:\Ampps\www\ncb.dev\public\u html\application\controllers\administrator\loans.php
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 );
}
}
第11行
函数:\u错误\u处理程序
文件:C:\Ampps\www\ncb.dev\public\u html\index.php
电话号码:315
功能:需要一次
我的模型
<?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);
}
}
通过向构造函数添加以下内容,我能够解决此问题:
$this->load->model('loan_m');
因此,承包商的外观如下所示:
public function __construct() {
parent::__construct();
$this->load->model('loan_m');
}
我看不到您的
Loans
控制器在哪里初始化Loans\m
类变量。这似乎就是为什么$this->load_m
为空。
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 );
}
}