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: 13
Tags: no tags
Ex_model Old
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. class Ex_Model extends CI_Model {
  6.  
  7.     protected $table_name = '';
  8.     protected $primary_key = 'id';
  9.     protected $primary_filter = 'intval';
  10.     public $order_by = '';
  11.     public $order = '';
  12.     public $rules = array();
  13.     public $limit = 0;
  14.     public $offset = 0;
  15.     public $where_column;
  16.  
  17.     public function __construct() {
  18.         parent::__construct();
  19.     }
  20.  
  21.     public function data_form_post($dataFields) {
  22.         $dataArray = array();
  23.         foreach ($dataFields as $field) {
  24.  
  25.             $dataArray[$field] = $this->input->post($field);
  26.         }
  27.         return $dataArray;
  28.     }
  29.  
  30.     public function get($id = null, $single = false) {
  31.         if ($id != null) {
  32.             $filter = $this->primary_filter;
  33.             $id = $filter($id);
  34.             if (!$id) {
  35.                 return false;
  36.             }
  37.             $this->db->where($this->primary_key, $id);
  38.             $method = 'row';
  39.             return $this->db->get($this->table_name)->$method();
  40.         } else if ($single == true) {
  41.             $method = 'row';
  42.             return $this->db->get($this->table_name)->$method();
  43.         } else {
  44.             $method = 'result';
  45.             return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
  46.         }
  47.     }
  48.  
  49.     public function get_by($wheres = array(), $single = false) {
  50.         foreach ($wheres as $key => $value) {
  51.             $this->db->where(trim($key), trim($value));
  52.         }
  53.         return $this->get(null, $single);
  54.     }
  55.  
  56.     public function get_order_by() {
  57.         $this->db->order_by($this->order_by, $this->order);
  58.         return $this->get();
  59.     }
  60.  
  61.     public function save($data, $id = null) {
  62.         if ($id == null) {
  63.             return $this->db->insert($this->table_name, $data);
  64.         } else {
  65.             $this->db->set($data);
  66.             $this->db->where($this->where_column, $id);
  67.             return $this->db->update($this->table_name);
  68.         }
  69.     }
  70.  
  71.     public function delete($id) {
  72.         $filter = $this->primary_filter;
  73.         $id = $filter($id);
  74.         if (!$id) {
  75.             return false;
  76.         }
  77.         $this->db->where($this->primary_key, $id);
  78.         $this->db->limit(1);
  79.         $this->db->delete($this->table_name);
  80.         return $this->db->affected_rows();
  81.     }
  82.  
  83.     public function delete_where($wheres = array(), $single = true) {
  84.         if (!empty($wheres)) {
  85.             $this->db->where($wheres);
  86.             if ($single) {
  87.                 $this->db->limit(1);
  88.             }
  89.             return $this->db->delete($this->table_name);
  90.         } else {
  91.             return false;
  92.         }
  93.     }
  94.  
  95.     public $search_column_name;
  96.  
  97.     public function get_numbers_of_rows($value = null) {
  98.         if (empty($this->search_column_name) || $this->search_column_name == null) {
  99.             $this->search_column_name = $this->primary_key;
  100.         }
  101.         return $value != null ? $this->db->where(trim($this->search_column_name), trim($value))->count_all_results($this->table_name) : $this->db->count_all($this->table_name);
  102.     }
  103.  
  104.     public function queryString($sqlString) {
  105.         return $this->db->query($sqlString)->row();
  106.     }
  107.  
  108.     public function get_where($column, $value) {
  109.         return $this->db->get_where($this->table_name, array($column => $value), $this->limit, $this->offset)->result();
  110.     }
  111.  
  112.     public function get_table_name() {
  113.         return $this->table_name;
  114.     }
  115.  
  116.     public function get_total_rows() {
  117.         return $this->db->get($this->table_name)->num_rows();
  118.     }
  119.  
  120.     public function count() {
  121.         return $this->db->count_all_results($this->table_name);
  122.     }
  123.  
  124. }
  125.