| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 | <?php/** * 扩展CI的基础模型 */class Base_Model extends CI_Model{    //protected $table;    public function __construct()    {        //如果没有设置表名,提示设置表名        if (!isset($this->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;        }    }}
 |