簡體   English   中英

PHP CodeIgniter錯誤:未定義屬性:模型

[英]PHP CodeIgniter Error: Undefined Property: Model

我試圖通過在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');加載可以在控制器構造函數或正在使用它的方法本身中進行的$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
}

看看這對您有何幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM