Codeigniter

CodeIgniter Model

Models deals with back end operations .Fetch data from database and send to controller. All database related manipulation is done by CI Model.
For ex –  Select, Fetching records, update and delete.
Location of  all model files is applications/models and CI model are control and accessed by controller.
How to use CI Models

  • Load database library
  • Define database configuration  in database.php file
  • Create Model
  • How to load model in Controller

Load database library

$this->load->database();

You have to define above library in every model if you want to reduce the lines of code then you can define database library in autoload.php file. Open application/config/autoload.php and add the database library in library array().

$autoload['libraries']=array('database');

How to Define database connection
Open applications/config/database.php and define database configuration.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost', // Your Hostname
	'username' => 'root', // Database User name
	'password' => '',     // Database password
	'database' => 'DBNAME', // Database Name
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

How to create CI Model

  • Model Class will be created inside the application/model folder.
  • You can also create a new folder inside application/model and save your files in this folder.
  • Model class will be inherit  from its base class CI_Model.
  • Use active record database pattern.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Empdata_Model extends CI_Model{
public function getempdata(){
$query=$this->db->select('Columnname1,Columnname2,Columnname3,Columnname4')
                ->where('columnname','Column Value');
                 ->get('tablename');
return $query->result();
}
}

How to load model in controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Empdata extends CI_Controller{
public function index(){
$this->load->model('Empdata_Model');// model name
$this->Empdata_Model->getempdata();// Model method
}
}

Recommended Tutorials for you


Related posts

CodeIgniter Introduction

Anuj Kumar

How to fetch data in CodeIgniter

Anuj Kumar

Form Validation in CodeIgniter

Anuj Kumar

CodeIgniter Directory Structure

Anuj Kumar

Active Record in CodeIgniter

Anuj Kumar

CodeIgniter Helpers

Anuj Kumar

Data Insertion in CodeIgniter

Anuj Kumar

CodeIgniter Interview Questions and Answers

Anuj Kumar

CodeIgniter Plugins

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.