Codeigniter:未定义的变量:sp和尝试获取非对象的属性

时间:2016-03-09 09:08:48

标签: php codeigniter

我正在尝试将输入的值插入数据库,但我不断收到这两个错误未定义的变量:sp 尝试获取非对象的属性在同一行,我将在下面发布我的MVC以便您进行验证。任何帮助将不胜感激。

MODEL

<?php
class Sagepay_M extends MY_Model
{   
protected $_table_name = 'sagepay';
protected $_order_by = 'id';
public $rules = array(
    'id' => array(
        'field' => 'id', 
        'label' => 'ID', 
        'rules' => 'trim|callback__unique_name|required|xss_clean'
    ), 
    'sagepay_no' => array(
        'field' => 'sagepay_no', 
        'label' => 'sagepay_no', 
        'rules' => 'trim|required|xss_clean'
    ),

);

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

public function get_new(){
    $sp = new stdClass();
    $sp->id = '';
    $sp->sagepay_no = $this->getNextAutoId();
    return $sp; //UPDATE FROM ANSWER    
}

public function getNextAutoId(){
    $tem = $this->db->select('Max(id) as id')->get('naturescrm_sagepay')->result();
    $count = $tem[0]->id;
    $count++;
    $prefix = 'SAGE' ;
    return $prefix. str_pad($count, 4, '0', STR_PAD_LEFT); ;
} 
}
?>

查看

                    <div class="form-group">
                  <label class="col-md-4 control-label" for="sagepay_no">Sagepay ID</label>
                  <div  class="col-md-8"><?php echo form_input('sagepay_no', set_value('sagepay_no', $sp->sagepay_no),'class="form-control" id="sagepay_no" readonly="true"'); ?></div>
                </div>  

CONTROLLER

<?php
class Sagepay extends Agent_Controller
{
public function __construct ()
{
    parent::__construct();
    //datatable
    $this->load->model("customer_m");
    $this->load->model("call_log_m");
    $this->load->model("order_m");
    $this->load->model("sagepay_m");
    $this->load->library('table');
    $this->load->library('datatables');
}

//finish
public function edit ($id = null)
{

    $this->load->model("call_log_m");
    $this->load->model("order_m");
    $this->load->model("sagepay_m");

    if($this->input->post("submit_btn")){
    $rules = $this->sagepay_m->rules;
    $this->form_validation->set_rules($rules);

    if ($this->form_validation->run() == TRUE) {
        $data = $this->sagepay_m->array_from_post(array('id','sagepay_no'));
         $id = $this->sagepay_m->save($data, $id);
        redirect('agent/customer/edit/' .$id);
    }
    }
        $this->data['sp'] =  $this->sagepay_m->get_new($id);
        $this->data['subview'] = 'agent/customer/edit';
        $this->load->view('agent/_layout_main', $this->data);
    }

}
?>

2 个答案:

答案 0 :(得分:0)

您不会在模型中返回您的对象:

public function get_new(){
    $sp = new stdClass();
    $sp->id = '';
    $sp->sagepay_no = $this->getNextAutoId();   

    return $sp;
}

答案 1 :(得分:0)

不确定它是否能解决您的问题,但在您的控制器中,您将$ id作为get_new方法的参数:

 $this->data['sp'] =  $this->sagepay_m->get_new($id);

而在你的模型中,get_new方法没有它:

public function get_new(){