| <?php | 
| /** | 
|  * 扩展模型 | 
|  */ | 
| class MY_Model extends CI_Model{ | 
|   | 
|     protected $db_error = "数据操作发生错误,请稍后再试-_-!"; | 
|   | 
|     /** | 
|      * 构造函数 | 
|      * | 
|      * 加载数据库和日志类 | 
|      */ | 
|     public function __construct() | 
|     { | 
|         parent::__construct(); | 
|         $this->load->database(); | 
|          | 
|     } | 
|   | 
|     /** | 
|      * 把数据写入表 | 
|      * @param string $table 表名 | 
|      * @param array $arr 待写入数据 | 
|      */ | 
|     protected function create($table,$arr) | 
|     { | 
|         return $this->db->insert($table, $arr); | 
|     } | 
|   | 
|     /** | 
|      * 根据id读取一条记录 | 
|      * @param string $table 读取的表 | 
|      * @param int $id 主键id | 
|      * @return array 一条记录信息数组 | 
|      */ | 
|     protected function read($table,$id) | 
|     { | 
|         return $this->db->get_where($table, array('id' => $id))->row_array(); | 
|     } | 
|   | 
|     /** | 
|      * 按id返回指定列,id可以是批量 | 
|      * @param string $select 指定字段,例:'title, content, date' | 
|      * @param string $table 查询的目标表 | 
|      * @param int,array $id 主键id,或id数组 | 
|      * @return array 返回对象数组 | 
|      */ | 
|     protected function select_where($select,$table,$id) | 
|     { | 
|         $this->db->select($select); | 
|         $this->db->from($table); | 
|         if(is_array($id)){ | 
|             $this->db->where_in('id',$id); | 
|             return $this->db->get()->result(); | 
|         }else{ | 
|             $this->db->where('id',$id); | 
|             return $this->db->get()->result(); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 根据id更新数据 | 
|      * @param string $table 查询的目标表 | 
|      * @param int $id 主键id | 
|      * @param array $arr 新的数据 | 
|      */ | 
|     protected function update($table,$id,$arr) | 
|     { | 
|         $this->db->where('id',$id); | 
|         return $this->db->update($table,$arr); | 
|     } | 
|   | 
|     /** | 
|      * 删除数据 | 
|      * id可以是单个,也可以是个数组 | 
|      * @param string $table 查询的目标表 | 
|      * @param int|array $id 主键id,或id数组 | 
|      */ | 
|     protected function delete($table,$id) | 
|     { | 
|         if(is_array($id)){ | 
|             $this->db->where_in('id',$id); | 
|             return $this->db->delete($table); | 
|         } | 
|         return $this->db->delete($table, array('id' => $id)); | 
|     } | 
|      | 
|     /** | 
|      * 检测某字段是否已经存在某值 | 
|      *  | 
|      * 存在返回该记录的信息数组,否则返回false | 
|      * @param string $table 查询的目标表 | 
|      * @param string $field 条件字段 | 
|      * @param string $value 条件值 | 
|      * @return false,array 返回false或存在的记录信息数组 | 
|      */ | 
|     protected function is_exists($table,$field,$value) | 
|     { | 
|         $query = $this->db->get_where($table, array($field => $value)); | 
|         if($query->num_rows() > 0) | 
|             return $query->row_array(); | 
|         return false; | 
|     } | 
|   | 
|     /** | 
|      * 分页列出数据 | 
|      * @param string $table 表名 | 
|      * @param int $limit 记录数 | 
|      * @param int $offset 偏移量 | 
|      * @param string $sort_by 排序字段 默认id | 
|      * @param string $sort 排序 默认倒序desc,asc,random | 
|      * @param string,null where条件,默认为空 | 
|      * @return object 返回记录对象数组 | 
|      */ | 
|     protected function list_data($table,$limit,$offset,$sort_by='id',$sort='desc',$where=null) | 
|     { | 
|         if(! is_null($where)) { | 
|             $this->db->where($where); | 
|         } | 
|         $this->db->order_by($sort_by,$sort); | 
|         return $this->db->get($table,$limit,$offset)->result(); | 
|     } | 
|   | 
|     /** | 
|      * 总记录数 | 
|      * @param string $table 表名 | 
|      */ | 
|     protected function count($table) | 
|     { | 
|         return $this->db->count_all($table); | 
|     } | 
|   | 
|     /** | 
|      * 按条件统计记录 | 
|      * @param string $table 表名 | 
|      * @param string $where 条件 | 
|      */ | 
|     protected function count_where($table,$where) | 
|     { | 
|         $this->db->from($table); | 
|         $this->db->where($where); | 
|         $result = $this->db->get(); | 
|         return $result->num_rows(); | 
|     } | 
|   | 
|     /** | 
|      * 列出全部 | 
|      * @param string $table 表名 | 
|      */ | 
|     protected function list_all($table) | 
|     { | 
|         return $this->db->get($table)->result(); | 
|     } | 
|   | 
|     /** | 
|      * 列出全部根据条件 | 
|      * @param string $table 表名 | 
|      * @param string $where where条件字段 | 
|      * @param string $value where的值 | 
|      * @param string $sort_by 排序字段 | 
|      * @param string $sort 排序方式 | 
|      */ | 
|     protected function list_all_where($table,$where,$value,$sort_by='id',$sort='desc') | 
|     { | 
|         $this->db->from($table); | 
|         if($where!='' and $value!=''){ | 
|             $this->db->where($where,$value); | 
|         } | 
|         $this->db->order_by($sort_by,$sort); | 
|         return $this->db->get()->result(); | 
|     } | 
|   | 
|     /** | 
|      * 列出数据(两个表关联查询) | 
|      * @param string $select 查询字段 | 
|      * @param string $table1 表名1 | 
|      * @param string $table2 表名2 | 
|      * @param string $on 联合条件 | 
|      * @param int $limit 读取记录数 | 
|      * @param int $offset 偏移量 | 
|      * @param string $sort_by 排序字段,默认id | 
|      * @param string $sort 排序方式,默认降序 | 
|      * @param string $where 过滤条件 | 
|      * @param string $join_type 链接方式,默认left | 
|      */ | 
|     protected function list_data_join($select,$table1,$table2,$on,$limit,$offset,$sort_by="id",$sort='DESC',$where=null,$join_type='left') | 
|     { | 
|         $this->db->select($select); | 
|         $this->db->from($table1); | 
|   | 
|         if(! is_null($where)) { | 
|             $this->db->where($where); | 
|         } | 
|   | 
|         $this->db->join($table2,$on,$join_type); | 
|         $this->db->limit($limit,$offset); | 
|         $this->db->order_by($sort_by,$sort); | 
|         return $this->db->get()->result(); | 
|     } | 
|   | 
|     /** | 
|      * 设置状态 | 
|      * 状态字段必须是status | 
|      * @param string $table 表名 | 
|      * @param int $id 主键id的值 | 
|      * @param int $status 状态值 | 
|      */ | 
|     protected function set_status($table,$id,$status) | 
|     { | 
|         $this->db->where('id',$id); | 
|         $this->db->set('status',$status); | 
|         return $this->db->update($table); | 
|     } | 
|   | 
|     /** | 
|      * 析构函数 | 
|      * | 
|      * 关闭数据库连接 | 
|      */ | 
|     public function __destruct() | 
|     { | 
|         //$this->db->close(); | 
|     } | 
| } | 
|   | 
| /* End of file MY_Model.php */ | 
| /* Location: ./application/core/MY_Model.php */ |