Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Amit Sarker
Added: Mar 7, 2018 11:06 AM
Views: 7
Tags: no tags
Ex_Model New
  1. <?php
  2.  
  3. class Ex_Model extends CI_Model {
  4.  
  5.     protected $table_name = '';
  6.     protected $primary_key = 'id';
  7.     protected $primary_filter = 'intval';
  8.     public $order_by = '';
  9.     public $order = '';
  10.     public $rules = array();
  11.     public $limit = 0;
  12.     public $offset = 0;
  13.     public $where_column = 'id';
  14.     public $search_column_name;
  15.  
  16.     public function __construct() {
  17.         parent::__construct();
  18.     }
  19.  
  20.     public function data_from_post($dataFields = array()) {
  21.         $dataArray = array();
  22.         foreach ($dataFields as $field) {
  23.             $dataArray[$field] = $this->input->post($field);
  24.         }
  25.         return $dataArray;
  26.     }
  27.  
  28.     public function get($id = null) {
  29.         if ($id != null) {
  30.             $this->db->where($this->primary_key, intval($id));
  31.             $method = 'row';
  32.             return $this->db->get($this->table_name)->$method();
  33.         } else {
  34.             $method = 'result';
  35.             return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
  36.         }
  37.     }
  38.  
  39.     public function get_by_id($id) {
  40.         $this->db->where($this->primary_key, intval($id));
  41.         $method = 'row';
  42.         return $this->db->get($this->table_name)->$method();
  43.     }
  44.  
  45.     public function insert_data($data) {
  46.         return (!empty($data)) ? $this->db->insert($this->table_name, $data) : FALSE;
  47.     }
  48.  
  49.     public function update_data($data) {
  50.         if (!empty($data)) {
  51.             $this->db->set($data);
  52.             $this->db->where($this->where_column, $data[$this->where_column]);
  53.             return $this->db->update($this->table_name);
  54.         } else {
  55.             return FALSE;
  56.         }
  57.     }
  58.  
  59.     public function get_where($where = array(), $single = FALSE) {
  60.         if (!empty($where)) {
  61.             $this->db->where($where);
  62.             $method = ($single) ? 'row' : 'result';
  63.             return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
  64.         } else {
  65.             return null;
  66.         }
  67.     }
  68.  
  69.     public function delete($id) {
  70.         $filter = $this->primary_filter;
  71.         $id = $filter($id);
  72.         if (!$id) {
  73.             return FALSE;
  74.         }
  75.         $this->db->where($this->primary_key, $id);
  76.         $this->db->limit(1);
  77.         return $this->db->delete($this->table_name);
  78.     }
  79.  
  80.     public function delete_where($column_name, $value) {
  81.  
  82.         $this->db->where($column_name, $value);
  83.         return $this->db->delete($this->table_name);
  84.     }
  85.  
  86.     public function delete_all($where = array()) {
  87.  
  88.         if (!empty($where)) {
  89.             $this->db->where($where);
  90.         }
  91.         return $this->db->delete($this->table_name);
  92.     }
  93.  
  94.     public function set_deleted($id = 0) {
  95.         $this->db->set('is_deleted', 1);
  96.         $this->db->where($this->primary_key, $id);
  97.         return $this->db->update($this->table_name);
  98.     }
  99.  
  100.     public function is_column_value_exist($column_name = '', $value = '') {
  101.         if ((!empty($column_name) && (!empty($value)))) {
  102.             $this->db->where($column_name, $value);
  103.             $total_count = $this->db->get($this->table_name)->num_rows();
  104.             return ($total_count > 0) ? TRUE : FALSE;
  105.         } else {
  106.             return TRUE;
  107.         }
  108.     }
  109.  
  110.     public function is_column_value_exist_with_another_row($column_name = '', $value = '', $id = 0) {
  111.         if ((!empty($column_name) && (!empty($value)) && ($id > 0))) {
  112.             $this->db->where(array($column_name => $value, 'id!=' => $id));
  113.             $total_count = $this->db->get($this->table_name)->num_rows();
  114.             return ($total_count > 0) ? TRUE : FALSE;
  115.         } else {
  116.             return TRUE;
  117.         }
  118.     }
  119.  
  120.     public function form_validation_custom_with_callable_method($name = '', $label = '', $callback_function = '', $message = '') {
  121.         $ci = &get_instance();
  122.         $ci->form_validation->set_rules(
  123.                 $name, $label, array(
  124.             array($name . '_callable', array($this, $callback_function))
  125.                 )
  126.         );
  127.         $ci->form_validation->set_message($name . '_callable', $message);
  128.     }
  129.  
  130.     public function get_tables_fields() {
  131.         return $this->db->list_fields($this->table_name);
  132.     }
  133.  
  134.     public function insert_batch($data = null) {
  135.  
  136.         if (!empty($data)) {
  137.             return $this->db->insert_batch($this->table_name, $data);
  138.         } else {
  139.             return FALSE;
  140.         }
  141.     }
  142.  
  143.     public function get_table_name() {
  144.         return $this->table_name;
  145.     }
  146.  
  147.     public function update($data = array(), $conditions = array()) {
  148.         $this->db->set($data);
  149.         $this->db->where($conditions);
  150.         return $this->db->update($this->table_name);
  151.     }
  152.  
  153. }
  154.