0

I was trying to use a inherited model, by creating a model in core/ folder, and create a newer model that extends the model in core/ folder, but then, when I try to log in, I get this error:

Severity: Notice

Message: Undefined property: CL_Login::$M_Login

Filename: controllers/CL_Login.php

Line Number: 47

The form is showed correctly, and posts to the following controller CL_Login/VerifyLogin.

I have created the following controller:

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;
//      }   
}

And here's the model inside Model folder:

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);
    }
}

And here's the model inside core folder:

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

/**
 * Master model of SimplaCMS
 *
 * @author Akbar Syarif
 * @email [email protected]
 * @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 */

The controller receives the value from the textbox smoothly with no problem. However, I got the mentioned error above. What else should I check?

2
  • Is this running on a server on a Windows or MAC PC? Commented May 16, 2015 at 10:23
  • where did you loaded your model? Commented May 16, 2015 at 12:59

1 Answer 1

1

You have to ensure that you are loading the model which you can do in the your controllers constructor or in the method itself that is using it, by using $this->load->model('m_login');

And refer to it as ...

public function CheckPassword()
{
    $output = $this->m_Login->get_login($this->input->post('InputUsername'),$this->input->post('InputPassword'));
    print_r($output);
// More code here
}

See how that flies for you!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.