bouchnina

My_Model - CodeIgniter

Apr 11th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. <?php
  2.  
  3. class My_Model extends CI_Model{
  4.      protected $_table_name     = '';
  5.      protected $_primary_key    = '';
  6.      protected $_order_by       ;
  7.      protected $_filter         = 'intval';
  8.      protected $rules           = array();
  9.      protected $_timestamp      = FALSE;
  10.    
  11.      function __construct() {
  12.          parent::__construct();
  13.      }
  14.    
  15.      public function get($id = null , $single = false){
  16.          if($id!=NULL){
  17.              $filter = $this->_filter;
  18.              $id = $filter($id);
  19.              $this->db->where($this->_primary_key,$id);
  20.              $method = 'first_row';
  21.          }else if($single){
  22.              $method = 'first_row';
  23.          }else{
  24.              $method = 'result_array';
  25.          }      
  26.          
  27.          return $this->db->get($this->_table_name)->$method();
  28.      }
  29.      
  30.      public function get_by($where , $single = false){
  31.          
  32.          $this->db->where($where);
  33.          return $this->get(NULL , $single);
  34.      }
  35.      
  36.      public function save($data , $id=NULL){
  37.          if($this->_timestamp){
  38.              $now = date('Y-m-d H:i:s');
  39.              $id || $data['created'] = $now;
  40.              $data['modified'] = $now;
  41.          }
  42.          if($id === NULL){
  43.              // Inserer un nouveau enregistrement
  44.              !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
  45.              $this->db->set($data);
  46.              $this->db->insert($this->_table_name);
  47.              $this->db->insert_id();        
  48.          }else{
  49.              // update
  50.              $filter = $this->_filter;
  51.              $id = $filter($id);
  52.              $this->db->set($data);
  53.              $this->db->where($this->_primary_key,$id);
  54.              $this->db->update($this->_table_name);
  55.          }
  56.          
  57.         return $id;
  58.      }
  59.      
  60.      public function delete($id=null){
  61.         if(!$this->get($id))
  62.             return false;
  63.          $filter = $this->_filter;
  64.          $id = $filter($id);
  65.          if(!$id){
  66.              return false;
  67.          }
  68.          $this->db->where($this->_primary_key,$id);
  69.          $this->db->limit(1);
  70.          $this->db->delete($this->_table_name);
  71.          return true;
  72.      }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment