CodeIgniter Add,View,Update,Delete using third party libraries - Mostlikers

13 April, 2015

CodeIgniter Add,View,Update,Delete using third party libraries

CodeIgniter Add,View,Update,Delete using third party libraries. Recent i have used Jamie Rumbelow My model libraries file. It's really good stuff, It provides a full CRUD base to make developing database interactions easier and quicker. this model have default validation and callback function.

CodeIgniter My Model


Live Demo           Download

There few steps to install and access the file, Below i have described all the stuff.

Installation 

For using this libraries you need download codeigniter file with basic steps. then next download libraries file.

Libraries file download Link : https://github.com/jamierumbelow/codeigniter-base-model 

Paste all the files to your codeiginter project file application --> core.

(OR)

Just drag the file in your project file application --> libraries. Set with autoload library in your config file application --> config --> autoload.php.

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

How to use

Create a new model class with an appropriate name, that extends MY_Model OR set same file in appropriate names.


  • After syncs the files create new controller. extend class name CI_Model to MY_Model. Because codeiginter default set with CI_Model 
  • You no need to create every time Add,delete,insert SQL codes. 

Ex: 
class MY_Model extends CI_Model {
    protected $table            = NULL;    
    protected $primary_key      = 'id';
    protected $fields           = array();
    public    $original_path       = './uploads';
    protected $before_create    = array();
    protected $after_create     = array();
    protected $before_update    = array();
    protected $after_update     = array();
    protected $before_get       = array();
    protected $after_get        = array();
    protected $before_delete    = array();
    protected $after_delete     = array();
    protected $result_mode      = 'object';
    protected $validate         = array();
    protected $skip_validation  = FALSE;


$table - It will get based on load model ex: $this->load->model('site_users', 'users');
$primary-key - Use your table primary key name (id),
$field - it will get post data of array name ex: array("id","title", "published");
$original_path - image upload file path location name (uploads).

Callbacks

 

Callback function we will use many time to  get and send data in table. This could be adding timestamps, pulling in relationships or deleting dependent rows.

  • $before_create
  • $after_create
  • $before_update
  • $after_update
  • $before_get
  • $after_get
  • $before_delete
  • $after_delete

MY model methods

get -  It will get single record to matching with primary key
$this -> users ->users-> get($id);
// sql query
SELECT * FROM users WHERE ID = '$id';

get($key, $value) - get the records matching these where parameters
$this -> users ->users-> get('city','bangalore');
// sql query limit 1 
SELECT * FROM users WHERE city = 'bangalore';


get_all - Get all the record in table
$this -> users ->users-> get_all();
// sql query
SELECT * FROM users;


get_all($key,$value) - get all the records matching these where parameters.
$this -> users ->users-> get_all('city','bangalore');
// sql query
SELECT * FROM users WHERE city = 'bangalore';

insert - insert data into table
$user_info=array('name'=>'karthick','age'=>'22'); // table insert value
$this -> users ->users->insert($user_info);


update($id,$value) - update particular record value
$id="5" // table primary key
$user_info=array('name'=>'karthick','age'=>'22'); // table insert value
$this -> users ->users->update($id,$user_info);


delete($id) - delete the record matching with id or primary key.
$this -> users ->users->delete($id);

Demo User Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Model {
    
    public function __construct()
    {
        parent::__construct();
        $this -> load -> model('users_model', 'users');
    }   
    
    public function userchecking()
    {
        $login = array(
            'email'=> $this->input->post('email'),
            'password' => md5($this->input->post('password'))
        );
        if($this -> users->count_all_results($login)) 
              echo 'Login success';
        else :
              echo 'Invalid login';
        endif;          
    }

    
    public function add()
    { 
        $user_content = array(
                'name' =>   $this -> input -> post('name'),
            'phone' =>  $this -> input -> post('phone'),
            'email' =>  $this -> input -> post('email'),
            'password' => md5($this -> input -> post ('password'))
        );          
        $this -> users -> insert($user_content);
        
    }
    
    
    public function get()   
    {
        //Single user information
        $this->data['user_details']= $this->users->get('id','1');
        $this->load->view('profile', $this->data, FALSE);       
    }

    public function get_all()   
    {
        // all user information
        $this->data['user_details']= $this->users->get_all();
        $this->load->view('profile', $this->data, FALSE);       
    }

    public function delete()   
    {
        // delete user codeing
        $this->data['user_details']= $this->users->delete('id','1');
        $this->load->view('profile', $this->data, FALSE);       
    }   


}


Demo Users model

class Users_model extends MY_Model {

    public function __construct()
    {
        parent::__construct();
        $this -> table = 'regusers';
        $this->primary_key='id';
        $this -> result_mode = 'object';
    }   

}


Grantor

Reference :  Jamie Rumbelow's base model.


"Every new beginning comes from some other beginning's end."








Related Topics


No comments:

Post a Comment