PHP Thinkphp3.2 数据库封装操作

    # --------------------------------------------------------
    #                     数据库操作方法                
    # --------------------------------------------------------
    
    /**
     * 查询单条
     * @param   $model      string            模型名或实例化的模型
     * @param   $where      array/string      查询条件(默认为所有字段)
     * @param   $field      array/string      字段(默认为所有字段)
     * @param   $order      string            排序(默认为空)
     * @param   $relation   string            关联(默认不关联)
     * @return  $result     array             结果集 
     * @date    2016-5-19 14:25:57 
     * @author  luox
     */
    public function getOne($model = '', $where = true, $field = true, $order = null,$relation = false) {
        is_string($model) ? $model  =   M($model)  :  null;
        $field     ?  $model->field($field)        :  null;
        $where     ?  $model->where($where)        :  null;
        $order     ?  $model->order($order)        :  null;
        $relation  ?  $model->relation($relation)  :  null;
        $result    =  $model->find();
        return $result;
    }
    
    /**
     * 查询多条
     * @param   $model      string            模型名或实例化的模型
     * @param   $where      array|string      查询条件
     * @param   $field      array|string      字段(默认为所有字段)
     * @param   $order      string            排序(默认为空)
     * @param   $limit      string            指定查询和操作的数量(传入'10'或者'5,10')
     * @param   $relation   string            关联(默认不关联)
     * @param   $distinct   boolean|string    唯一(去掉重复)
     * @param   $group      boolean|string    分组
     * @return  $result     array             结果集
     * @date    2016-5-19 14:25:57
     * @author  luox
     */
    public function getData($model = '', $where = true, $field = true, $order = null, $limit = false, $relation = false, $distinct = false, $group = false) {
        is_string($model) ? $model  =   M($model)   :  null;
        $field      ?  $model->field($field)        :  null;
        $where      ?  $model->where($where)        :  null;
        $order      ?  $model->order($order)        :  null;
        $limit      ?  $model->limit($limit)        :  null;
        $relation   ?  $model->relation($relation)  :  null;
        $distinct   ?  $model->distinct($distinct)  :  null;
        $group      ?  $model->group($group)        :  null;
        $result     =  $model->select();
        return $result;
    }
    
    /**
     * 统计总数
     * @param   $model      string          模型名或实例化的模型
     * @param   $where      array/string    查询条件
     * @param   $distinct   boolean|string  唯一(去掉重复)
     * @return  $result     array           记录数
     * @date    2016-5-19 14:25:57   
     * @author  luox
     */
    public function countData($model = '', $where = true,  $name = '*', $distinct = false) {
        is_string($model) ? $model  =   M($model)        :  null;
        $distinct         ? $model->distinct($distinct)  :  null;
        $result = $model->where($where)->count($name);
        return $result;
    }
    
    /**
     * 新增单条
     * @param   $model      string   模型名或实例化的模型
     * @param   $data       array    数据
     * @return  $result     array    新增记录ID值
     * @date    2016-5-19 14:25:57
     * @author  luox
     */
    public function addOne($model = '', $data = '') {
        is_string($model) ? $model  =   M($model)  :  null;
        $result = $model->add($data);
        return $result;
    }
    
    /**
     * 新增多条
     * @param   $model      string    模型名或实例化的模型
     * @param   $data       array     数据
     * @return  $result     array     新增记录最后一条ID值
     * @date    2016-5-19 14:25:57  
     * @author  luox
     */
    public function addData($model = '', $data = '') {
        is_string($model) ? $model  =   M($model)  :  null;
        $result = $model->addAll($data);
        return $result;
    }
    
    /**
     * 修改
     * @param   $model      string         模型名或实例化的模型
     * @param   $where      array/string   条件
     * @param   $data       array          数据
     * @return  $result     array          影响记录数  
     * @author  luox
     */
    public function saveData($model = '', $where = true, $data = '') {
        is_string($model) ? $model  =   M($model)  :  null;
        $result = $model->where($where)->save($data);
        return $result;
    }
    
    /**
     * 删除
     * @param   $model      string         模型名或实例化的模型
     * @param   $where      array/string   条件(不能为空)
     * @return  $result     array          删除记录数  
     * @author  luox
     */
    public function delData($model = '', $where = true) {
        is_string($model) ? $model  =   M($model)  :  null;
        $result = $model->where($where)->delete();
        return $result;
    }
    
    /**
     * 获取特定字段
     * @param   $model      string         模型名或实例化的模型(不能为空)
     * @param   $where      array/string   条件(不能为空)
     * @param   $field      string         字段名(不能为空)
     * @param   $spea       string         字段数据间隔符号 NULL返回数组
     * @return  $result     array          删除记录数  
     * @date    2016-5-23 10:09:41
     * @author  luox
     */
    public function getCol($model = '', $where = true, $field, $sepa=false) {
        is_string($model) ? $model  =   M($model)  :  null;
        $result = $model->where($where)->getField($field,$sepa);
        return $result;
    }
    
    /**
     * 原生SQL查询
     * 注意query返回的是二维数组,返回单条第二个参数给个true
     * @param   $sql        string         表名
     * @param   $one        boolean        是否返回结果集第一条 
     * @date    2016-5-23 10:09:41
     * @author  luox
     */
    public function runSql($sql = '', $one = false) {
        //实例化空模型
        $model = new Model();
        //进行原生的SQL查询
        $result = $model->query($sql);
        //第二个参数为true,则返回第一条数据,否则返回原生sql查询结果
        return !$one ? $result : $result[0];
    }

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Back To Top

鄂ICP备17008157号-1