Model
What is a Model?

Models is Class that inherit CI_Model. When are work with database like insert record,update record etc. so we create function in Model class and call from controller.We save Model classes in application/models/ folder. Model class name and model page name should be same like controller. you can not directly access model, you can run only from controller. You can see by default there is no any model.

How to create Model

Create this class file and save this application->models, And name of class name and page name should be same and start with capital letter.

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

class User_Model extends CI_Model {

public function showuser()
{
echo "This is show user function";
}

}

How to access Model.

We call model from controller. 

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

class User extends CI_Controller {

public function index()
{
$this->load->model('user_model');
$this->user_model->showuser();
}

}

Here we load and call model from user controller

For load model :  $this->load->model('user_model');  

For access model : $this->user_model->showuser();


Previous
Next