Codeigniter无法加载模型

时间:2014-09-28 07:53:38

标签: php codeigniter

controllers / verify_login.php page

   <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Verify_login extends CI_Controller {

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

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('admin/index');
   }
   else
   {
     //Go to private area
     redirect('about', 'refresh');
   }

 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');

   //query the database

   $result = $this->Admin_Model->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}

models / adminl_model.php page

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Adminl_Model extends CI_Model
{
     function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
 function login($username, $password)
 {
   $this -> db -> select('id, username, password','type');
   $this -> db -> from('users');
   $this -> db -> where('nick', $username);
   $this -> db -> where('type', 'admin');
   $this -> db -> where('pass', MD5($password));
   $this -> db -> limit(1);

   $query = $this -> db -> get();

   if($query -> num_rows() == 1)
   {
     return $query->result();
   }
   else
   {
     return false;
   }
 }

}

给出错误。我该如何解决这个问题?我正在研究c9.io.和stackoverflow说这是如此的代码。我理解我的问题太荒谬了。

2 个答案:

答案 0 :(得分:1)

使用$this->admin_model->login($username, $password);代替$this->Admin_Model->login($username, $password);

加载后,您将使用与您的班级同名的对象访问您的模型函数:

此处您已将模型加载为$this->load->model('adminl_model') ;,因此您可以使用adminl_model而不是Admin_Model

答案 1 :(得分:1)

这段代码:

$result = $this->Admin_Model->login($username, $password);

必须:

$result = $this->adminl_model->login($username, $password);