我试图使用继承模型,通过在core/
文件夹中创建一个模型,并创建一个扩展core/
文件夹中模型的新模型,但是,当我尝试登录时,我得到这个错误:
Severity: Notice
Message: Undefined property: CL_Login::$M_Login
Filename: controllers/CL_Login.php
Line Number: 47
表单显示正确,并发送到以下控制器CL_Login/VerifyLogin
。
我已经创建了以下控制器:
public function VerifyLogin()
{
$this->form_validation->set_rules('InputUsername', 'Username', 'required');
$this->form_validation->set_rules('InputPassword', 'Password', 'required|callback_CheckPassword');
if ($this->form_validation->run())
{
echo 'login sukses';
}
else
{
echo 'login gagal';
}
}
public function CheckPassword()
{
$output = $this->M_Login->get_login($this->input->post('InputUsername'),$this->input->post('InputPassword'));
print_r($output);
// if($output)
// {
// return true;
// }
// else
// {
// return false;
// }
}
下面是Model
文件夹中的模型:
class M_Login extends MY_Model {
protected $table = 'ms_user';
protected $primary_key = 'user_id';
public function __construct()
{
parent::__construct();
}
public function get_login($query = NULL, $username, $password)
{
$query['where']['user_name'] = $username;
$query['where']['user_password'] = md5($password);
return $this->get($query);
}
}
下面是core
文件夹中的模型:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Master model of SimplaCMS
*
* @author Akbar Syarif
* @email aksa.uncp@gmail.com
* @package SimplaCMS
*/
class MY_Model extends CI_Model {
// table
protected $table;
// primary key
protected $primary_key;
// error status
protected $error = FALSE;
// error message
protected $error_message = array();
public function __construct()
{
parent::__construct();
}
/**
* Add custom data to table
*
* @param Array (data)
* @return boolen. true if data successfully save and false if error occured
*/
public function insert( $data = NULL )
{
// if data not set
if ( is_null($data) ) {
$this->error = TRUE;
throw new Exception("The first parameter cannot be empty");
}
// if data not array
if ( ! is_array($data) ) {
$this->error = TRUE;
throw new Exception("The first parameter must be an array");
}
if ( ! $this->error ) {
$this->db->insert($this->table, $data);
} else {
return FALSE;
}
}
public function insert_batch( $data = NULL )
{
// if data not set
if ( is_null($data) ) {
$this->error = TRUE;
throw new Exception("The first parameter cannot be empty");
}
// if data not array
if ( ! is_array($data) ) {
$this->error = TRUE;
throw new Exception("The first parameter must be an array");
}
if ( ! $this->error ) {
$this->db->insert_batch($this->table, $data);
} else {
return FALSE;
}
}
/**
* Get row
* @param Array (data)
* @return mixed. return false if nothing row found,
* return object if there's at least one row found
*/
private function _get( $query = NULL )
{
if(isset($query['select'])) $this->db->select($query['select']);
if(isset($query['where'])) $this->db->where($query['where']);
if(isset($query['where_no_escaped'])) $this->db->where($query['where_no_escaped'], NULL, FALSE);
if(isset($query['or_where'])) $this->db->or_where($query['or_where']);
if(isset($query['or_where_no_escaped'])) $this->db->or_where($query['or_where_no_escaped'], NULL, FALSE);
if(isset($query['like'])) $this->db->like($query['like']);
if(isset($query['order_by'])) $this->db->order_by($query['order_by']);
if(isset($query['limit'])) $this->db->limit($query['limit']);
if(isset($query['limit_offset'])) $this->db->limit($query['limit_offset'][0], $query['limit_offset'][1]);
// join table
if(isset($query['join'])) {
if ( ! is_array($query['join']) ) {
$this->error = TRUE;
throw new Exception("Join value must be an array");
} else {
foreach ($query['join'] as $key => $value) {
$this->db->join($value[0], $value[1], $value[2]);
}
}
}
// return result
if ( ! $this->error ) {
$result = $this->db->get($this->table);
} else {
$result = FALSE;
}
return $result;
}
public function get( $query = NULL )
{
$result = $this->_get($query)->result();
return $result;
}
public function row( $query = NULL )
{
$result = $this->_get($query)->row();
return $result;
}
public function count( $query = NULL )
{
$result = $this->_get($query)->num_rows();
return $result;
}
/**
* Delete row by primary key
* @param int. Primary Key
* @return boolean. return true if row successfully delete
*/
public function delete( $primary_key = NULL )
{
// if primary key not set
if ( is_null($primary_key) ) {
$this->error = TRUE;
throw new Exception("First parameter cannot be empty.");
}
// if nothing error
if ( ! $this->error ) {
$this->db
->where($this->primary_key, $primary_key)
->delete($this->table);
} else {
return FALSE;
}
}
/**
* Update row by primary key
* @param array. Custom data
* @param int. Primary Key
* @return boolen. return true if row successfully update
*/
public function update( $data, $primary_key )
{
// if first argument not set or not an array
if ( func_num_args() == 0 ) {
$this->error = TRUE;
throw new Exception("First parameter cannot be empty");
} else {
if ( ! is_array($data) ) {
$this->error = TRUE;
throw new Exception("First parameter must be an array");
}
}
// if second parameter not set
if ( func_num_args() == 0 ) {
$this->error = TRUE;
throw new Exception("First parameter cannot be empty");
}
// if nothing error
if ( ! $this->error ) {
$this->db
->set($data)
->where($this->primary_key, $primary_key)
->update($this->table);
} else {
return FALSE;
}
}
}
/* End of file MY_Model.php */
/* Location: ./application/models/MY_Model.php */
控制器顺利接收到文本框中的值,没有问题。然而,我得到了上面提到的错误。我还需要检查什么?
你必须确保你正在加载模型你可以在你的控制器构造函数中或者在使用它的方法本身中做,通过使用$this->load->model('m_login');
并将其称为…
public function CheckPassword()
{
$output = $this->m_Login->get_login($this->input->post('InputUsername'),$this->input->post('InputPassword'));
print_r($output);
// More code here
}
看看这是怎么回事!