我试图将1个模型用于许多控制器和视图,因为除了4个变量之外,模型具有共同的一切,为此我尝试使用var_setter()
函数初始化模型中的变量模型并在模型加载到控制器后调用它,但它显示以下错误:
以及下面给出的模型和控制器的片段以及视图工作正常: 模型:
class Person_model extends CI_Model {
var $table ='';
var $column_order = array();
var $column_search = array();
var $order = array();
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function var_setter($table,$column_order,$column_search,$order){
$this->$table = $table;
$this->$column_order = $column_order;
$this->$column_search = $column_search;
$this->$order = $order;
}
?>
控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Person extends CI_Controller {
var $table = 'persons';
var $column_order = array('date_for_contract','contract_contract_no','contract_company_name' ,'contract_created_by','contract_payment_base','contract_billing_cycle','contract_payment_as_per','contract_pay_mode','contract_credit_limit','contract_stax_paid_by','contract_agreed_payment_days',' contract_loading' ,'contract_unloading','contract_reporting_time','contract_packing_mode','contract_location','contract_diesel_rate_on_contract_day','contract_diesel_rate_variation_by_rate','contract_diesel_rate_variation_by_percentage','contract_start_date','contract_end_date','business_relation_start','risk_name','risk_amount','insurance_cover','contract_remark','contract_from','contract_destination','contract_transit_time','contract_mode','load_limit_from','load_limit_to','contract_rate',null); //set column field database for datatable orderable
var $column_search = array('date_for_contract','contract_contract_no','contract_from','contract_destination','contract_mode'); //set column field database for datatable searchable just firstname , lastname , address are searchable
var $order = array('id' => 'desc'); // default order
public function __construct()
{
parent::__construct();
$this->load->model('person_model','person');
$this->person->var_setter($this->$table, $this->$column_order, $this->$column_search, $this->$order);
}
public function index()
{
$this->load->view('person_view');
}
?>
答案 0 :(得分:1)
像这样更改Person_model&#39; var_setter
public function var_setter($table,$column_order,$column_search,$order){
$this->table = $table;
$this->column_order = $column_order;
$this->column_search = $column_search;
$this->order = $order;
}
然后在Person
控制器的构造方法
$this->person->var_setter($this->table, $this->column_order, $this->column_search, $this->order);