Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class My_Model extends CI_Model{
- protected $_table_name = '';
- protected $_primary_key = '';
- protected $_order_by ;
- protected $_filter = 'intval';
- protected $rules = array();
- protected $_timestamp = FALSE;
- function __construct() {
- parent::__construct();
- }
- public function get($id = null , $single = false){
- if($id!=NULL){
- $filter = $this->_filter;
- $id = $filter($id);
- $this->db->where($this->_primary_key,$id);
- $method = 'first_row';
- }else if($single){
- $method = 'first_row';
- }else{
- $method = 'result_array';
- }
- return $this->db->get($this->_table_name)->$method();
- }
- public function get_by($where , $single = false){
- $this->db->where($where);
- return $this->get(NULL , $single);
- }
- public function save($data , $id=NULL){
- if($this->_timestamp){
- $now = date('Y-m-d H:i:s');
- $id || $data['created'] = $now;
- $data['modified'] = $now;
- }
- if($id === NULL){
- // Inserer un nouveau enregistrement
- !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
- $this->db->set($data);
- $this->db->insert($this->_table_name);
- $this->db->insert_id();
- }else{
- // update
- $filter = $this->_filter;
- $id = $filter($id);
- $this->db->set($data);
- $this->db->where($this->_primary_key,$id);
- $this->db->update($this->_table_name);
- }
- return $id;
- }
- public function delete($id=null){
- if(!$this->get($id))
- return false;
- $filter = $this->_filter;
- $id = $filter($id);
- if(!$id){
- return false;
- }
- $this->db->where($this->_primary_key,$id);
- $this->db->limit(1);
- $this->db->delete($this->_table_name);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment