代码点火器 - 调用从一个或多个模型到控制器的多个功能(方法)会导致致命错误

时间:2015-09-04 11:45:53

标签: codeigniter-3

**My Controller File Name is Members.php and the code is...**
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Members extends CI_Controller {

    public function __construct() 
       {
            parent::__construct();            
            //$this->load->model(array('Nationality_model','Religion_model'));  I tried this too

       }

    public function index()
    {
        $this->load->view('members/home');
    }

    public function customers()
    {

        if ($this -> session -> userdata('user_mallname') != "" )
        {

                $this->load->helper(array('form', 'url'));
                $this->load->helper('security');
                $this->load->library('form_validation');

                $this -> form_validation -> set_rules('fullname', 'Full Name', 'trim|required|min_length[4]|max_length[40]|xss_clean|alpha_numeric');

                if ($this->form_validation->run() == FALSE)
                {

                    //$this -> load -> model('Nationality_model');
                    //$data['nat_list'] = $this -> Nationality_model -> natlist();

                    //$this -> load -> model('Religion_model');
                    //$data['reg_list'] = $this -> Religion_model -> reglist();


                    $this -> load -> model('Member_model');
                    $data['nat_list'] = $this -> Member_model -> natlist();
                    $data['nat_list'] = $this -> Member_model -> reglist();

                    $this->load->view('members/user-new', $data);
                }
                else
                {                   
                    // code goes here
                }

        }else{
            redirect('members/login', 'refresh');
        }
    }
}

我的模型member_model.php中的代码是

<?php
 class Member_model extends CI_Model
 {

     public function __construct() {
         parent::__construct();
         $this -> load -> helper('mysqli'); // we are using Mysql Database with Stored Procedures
     }

     //NAT LIST  START    --  this function copied from    Nationality_model.php
     public function natlist() {      

         $sql = "CALL usp_natList()";        
         $parameters = array();
         $query = $this -> db -> query($sql, $parameters);  
         return $query -> result();
     }

     //NAT LIST END

     //REG LIST  START this function copied from Religion_model.php
     public function reglist() {

         $sql = "CALL usp_reglist()";        
         $parameters = array();
         $query = $this -> db -> query($sql, $parameters);  
         return $query -> result();
     }
     //REG LIST END
}

我试过了1.从一个模型调用2.从两个不同模型调用。 所有都显示 致命错误:在非对象上调用成员函数result() 在本地主机WAMP中发生错误。我在stackoverflow中做了R&amp; D,我无法找到答案。如果我的代码有任何问题,请告诉我。提前谢谢。
注意:如果我只从任何模型加载一个方法。它工作正常。 !!

1 个答案:

答案 0 :(得分:0)

确保您已自动加载数据库&#39; config.php中的库

$autoload['libraries'] = array('database');

或将其加载到控制器构造函数中。

...
parent::__construct();            
$this->load->database();
$this->load->model(array('Nationality_model','Religion_model')); $this->load->database();
...