table)) { throw new Exception ('please setup $table property with this model first'); } parent::__construct(); } /** * 查询单条记录 * * @param array $where * @param string $fields * * PS : 可根据业务场景扩展 */ public function get_one($where, $fields='*', $order=NULL) { if (!empty($where)) { if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } } return $this->db->select($fields)->order_by($order)->get($this->table)->row_array(); } /** * 新增 * * @param array $data 新增数据 * * @return int $res 返回成功插入记录对应的ID */ public function add($data) { $this->db->insert($this->table, $data); return $this->db->insert_id(); } /** * 批量新增 * * @param array $data 新增数据 * * @return int $res Number of rows inserted or FALSE on failure */ public function add_batch($data) { return $this->db->insert_batch($this->table, $data); } /** * 替换 * * @param array $data 替换数据 * * @return int $res 返回成功插入记录对应的ID */ public function replace($data) { $this->db->replace($this->table, $data); return $this->db->insert_id(); } /** * 更新 * * @param array $data 更新字段 * @param array $where 判定条件 */ public function update($data, $where) { if (!empty($where)) { if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } return $this->db->update($this->table, $data); } return false; } /** * 删除 */ public function delete($where) { if (!empty($where)) { if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } return $this->db->delete($this->table); } return false; } /** * 获取数量 * * @param mixed $where * @param string $fields * @param int $limit * @param int $offset * @return array */ public function get_count($where=NULL) { if(!empty($where)){ if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } } return $this->db->count_all_results($this->table); } /** * 获取列表 * * @param mixed $where * @param string $fields * @param int $limit * @param int $offset * @return array */ public function get_list($where=NULL, $fields='*',$limit=NULL, $offset=NULL, $order=NULL, $group=NUll) { $this->db->select($fields); if(!empty($where)){ if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } } if(!empty($order)){ $this->db->order_by($order); } if(!empty($group)){ $this->db->group_by($group); } $query = $this->db->get($this->table, $limit, $offset); return $query->result_array(); } /** * 公用方法 联查 * * @param array $where * @param string $fields * @param int $limit * @param int $offset * @param array $join * |- string table * |- string cond 关联条件 * |- string type 联查方式 ep:'left, right inner' * * @param order * @param group * @param $alias 为主表设置别名 * @return array */ public function get_list_by_join($where=NULL, $fields='*',$limit=NULL, $offset=NULL, $join = NULL, $order=NULL, $group=NUll, $alias=NULL) { $this->db->select($fields); $table = $this->table; if(!empty($where)){ if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { if (trim($value) !== '') { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field) { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } } else { $this->db->where($where); } } if($join !== NULL){ if (count($join) == count($join, 1)) { $this->db->join($join['table'], $join['cond'], $join['type']); } else { foreach ($join as $val) { $this->db->join($val['table'], $val['cond'], $val['type']); } } } if(!empty($order)){ $this->db->order_by($order); } if(!empty($group)){ $this->db->group_by($group); } $this->db->limit($limit, $offset); if (!empty($alias)) { $table .= ' as '.$alias; } $query = $this->db->get($table); // var_dump($this->db->last_query()); return $query->result_array(); } /** * 公用方法, 多表联查 * * @param array $where * @param string $fields * @param int $limit * @param int $offset * @param array $join 二维关联数组 * array * |- string table * |- string cond 关联条件 * |- string type 联查方式 ep:'left, right inner' * @param string $order * @param string $group * @param string $alias * * @return array */ public function get_list_by_multi_join($where=NULL, $fields='*',$limit=NULL, $offset=NULL, $join = NULL, $order=NULL, $group=NUll, $alias=NULL, $is_row = false) { $this->db->select($fields); $table = $this->table; if(!empty($where)){ if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } } if($join !== NULL) { foreach ($join as $val) { $this->db->join($val['table'], $val['cond'], $val['type']); } } if (!empty($alias)) { $table .= ' as '.$alias; } if(!empty($order)){ $this->db->order_by($order); } if(!empty($group)){ $this->db->group_by($group); } $this->db->limit($limit, $offset); $query = $this->db->get($table); $result = $is_row ? $query->row_array() : $query->result_array(); // var_dump($this->db->last_query()); return $result; } /* 多联查获取单个 */ public function get_one_by_multi_join($where=NULL, $fields='*',$join = NULL, $order=NULL, $group=NUll, $alias=NULL) { $res = $this->get_list_by_multi_join($where, $fields, 1, 0, $join, $order, $group, $alias); return $res[0]; } /* 单联查获取单个 */ public function get_one_by_join($where=NULL, $fields='*',$join = NULL, $order=NULL, $group=NUll, $alias=NULL) { $res = $this->get_list_by_join($where, $fields, 1, 0, $join, $order, $group, $alias); return $res[0]; } // 判断数据是否已经存在 public function getDataCount($where, $id = 0) { if(!empty($where)){ if (is_array($where)) { foreach ($where as $key => $value) { if (is_array($value)) { $value = !empty($value) ? $value : [0]; $this->db->where_in($key, $value); } else { if (strpos($key, '|') === false){ $this->db->where($key, $value); } else { $this->db->group_start(); $key_arr = explode('|', $key); foreach ($key_arr as $field) { if ($field && $value !== '') { $this->db->or_like($field, $value); } } $this->db->group_end(); } } } } else { $this->db->where($where); } } if (!empty($id)) { $this->db->where('id !=',$id); } $query = $this->db->get($this->table); $data = $query->row_array(); if (empty($data)) { return 0; } else { return $id == $data['id'] ? 0 : 1; } } }