用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2462|回复: 1
收起左侧

[模型] 分享几个sql常用的封装方法

[复制链接]
发表于 2017-10-28 16:43:03 | 显示全部楼层 |阅读模式
直接上代码
/**
         * 单条记录 关联查询
         *
         * @param string|表名   $name
         * @param string|查询字段 $where
         * @param string      $select
         * @param string      $relation 关联添加
         * @return mixed
         */
        public function get_row_join($name, $where, $select = '*', $relation = '')
        {
                $this->db->select($select);
                $this->db->from($name);
                if ($relation) {
                        if (isset($relation[0])) {
                                foreach ($relation as $item) {
                                        $this->db->join($item['table'], $item['condition'], $item['type']);
                                }
                        } else {
                                $this->db->join($relation['table'], $relation['condition'], $relation['type']);
                        }
                }
                $where && $this->db->where($where);
                return $this->db->get()->row_array();
        }

        /**
         * 获取多条记录 关联查询
         *
         * @param        $name
         * @param array  $where
         * @param string $order_by
         * @param string $limit
         * @param string $select
         * @param string $relation 关联添加
         * @return mixed
         */
        public function get_result_join($name, $where = array(), $order_by = '', $limit = '', $select = '*', $relation = '')
        {
                $this->db->select($select);
                $this->db->from($name);
                if ($relation) {
                        if (isset($relation[0])) {
                                foreach ($relation as $item) {
                                        $this->db->join($item['table'], $item['condition'], $item['type']);
                                }
                        } else {
                                $this->db->join($relation['table'], $relation['condition'], $relation['type']);
                        }
                }
                $where && $this->db->where($where);
                $order_by && $this->db->order_by($order_by);
                $limit && $this->db->limit($limit[0], $limit[1]);
                return $this->db->get()->result_array();
        }

        /**
         * 查询总数量
         *
         * @param string $table
         * @param array  $where
         * @return string
         */
        public function counts($table = '', $where = array())
        {
                $where && $this->db->where($where);

                return $this->db->count_all_results($table);
        }

        /**
         * 更新表字段
         *
         * @param string $table
         * @param string $id
         * @param string $point       //要更新的字段
         * @param string $point_after //要更新的数值
         * @return mixed
         */
        public function set($table = '', $id = '', $point = '', $point_after = '')
        {
                return $this->db->where('id', $id)->set($point, $point_after, false)->update($table);
        }

        /**
         * 获取单条数据
         *
         * @author markYang
         *
         * @param $table      表名
         * @param $fields     查询字段
         * @param $whereParam 条件数组
         * @param $condParam  array('order'=>'', 'group'=>'')
         * @return 以数组格式返回数组
         */
        public function get_row($table = '', $fields = '*', $whereParam = array(), $condParam = array())
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');
                //检测并初始化查询字段参数
                empty($fields) || $this->db->select($fields);
                //检测条件参数数组
                empty($whereParam) || $this->db->where($whereParam);
                //检测并初始化ORDER BY 参数
                !empty($condParam['orderby']) && $this->db->order_by($condParam['orderby']);
                //检测并初始化GROUP BY 参数
                isset($condParam['groupby']) && $this->db->group_by($condParam['groupby']);

                //执行并以数组格式返回数据
                return $this->db->get($table, 1)->row_array();
        }

        /**
         * 获取多条数据
         *
         * @author markYang
         *
         * @param $table      表名
         * @param $fields     查询字段
         * @param $whereParam 条件数组
         * @param $condParam  array('orderby'=>'', 'groupby'=>'', 'limit'=>'')
         * @return 以数组格式返回数组
         */
        public function get_rows($table = '', $fields = '*', $whereParam = array(), $condParam = array())
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');
                //检测并初始化查询字段参数
                empty($fields) || $this->db->select($fields);
                //检测并初始化条件参数数组
                empty($whereParam) || $this->db->where($whereParam);
                //检测并初始化ORDER BY 参数
                !empty($condParam['orderby']) && $this->db->order_by($condParam['orderby']);
                //检测并初始化GROUP BY 参数
                !empty($condParam['groupby']) && $this->db->group_by($condParam['groupby']);
                //检测获取数据条数
                if (!empty($condParam['limit'])) {
                        $limitArr = explode(',', $condParam['limit']);
                        empty($limitArr[1]) ? $this->db->limit($limitArr[0]) : $this->db->limit($limitArr[1], $limitArr[0]);
                }

                //执行并以数组格式返回数据
                return $this->db->get($table)->result_array();
        }

        /**
         * 获取多条数据
         *
         * @author markYang
         *
         * @param $sql sql语句
         * @return 以数组格式返回数组
         */
        public function get_rows_query($sql = '')
        {

                //检测SQL参数
                empty($sql) && show_error('SQL语句不能为空!');

                //执行并以数组格式返回数据
                return $this->db->query($sql)->result_array();
        }

        /**
         * 添加数据信息
         *
         * @author markYang
         *
         * @param $table    表名
         * @param $setParam 需要修改的信息数组
         * @return 返回执行结果
         */
        public function insert($table = '', $setParam = array())
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');
                //检测修改信息数组参数
                empty($setParam) && show_error('Set up information cannot be empty');
                //执行并返回执行结果
                if ($this->db->insert($table, $setParam)) {
                        return $this->db->insert_id();
                } else {
                        return false;
                }
        }

        /**
         * 批量添加数据
         *
         * @author markYang
         *
         * @param $table    表名
         * @param $setParam 需要修改的信息数组
         * @return 返回执行结果
         */
        public function insert_batch($table = '', $setParam = array())
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');
                //检测修改信息数组参数
                empty($setParam) && show_error('Set up information cannot be empty');
                //执行并返回执行结果
                return $this->db->insert_batch($table, $setParam);
        }

        /**
         * 修改数据信息
         *
         * @author markYang
         *
         * @param $table      表名
         * @param $setParam   需要修改的信息数组
         * @param $whereParam 条件信息数组
         * @param $limit      限制数量
         * @return 返回执行结果
         */
        public function update_row($table = '', $setParam = array(), $whereParam = array(), $limit = null)
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');
                //检测修改信息数组参数
                empty($setParam) && show_error('Set up information cannot be empty');
                //检测条件信息数组参数
                empty($whereParam) && show_error('Condition parameters cannot be empty');

                //执行并返回执行结果
                $this->db->update($table, $setParam, $whereParam, $limit);
                return $this->db->affected_rows();
        }

        /**
         * 批量修改数据信息
         *
         * @author markYang
         *
         * @param $table    表名
         * @param $setParam 需要修改的数值数组
         * @param $index    修改修改的字段名
         * @return 返回执行结果
         */
        public function update_batch($table = '', $setParam = array(), $index = '')
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');
                //检测修改信息数组参数
                empty($setParam) && show_error('Set up information cannot be empty');
                //检测条件信息数组参数
                empty($index) && show_error('the value of index cannot be empty');

                //执行并返回执行结果
                return $this->db->update_batch($table, $setParam, $index);
        }

        /**
         * 删除数据信息
         *
         * @author markYang
         */
        public function delete($table = '', $whereParam = array(), $limit = null, $reset_data = true)
        {

                //检测表名参数
                empty($table) && show_error('The table name cannot be empty');

                //执行并返回执行结果
                return $this->db->delete($table, $whereParam, $limit, $reset_data);
        }

可以新建一个类,放在里面用,基本都是常用的
发表于 2017-10-29 13:41:50 | 显示全部楼层
好顶赞,收藏了

本版积分规则