zoukankan      html  css  js  c++  java
  • ConfigService升级版

    <?php
    /**
    * User: Eden
    * Date: 2019/3/30
    * 共有内容
    */
    
    /**
     CREATE TABLE `tf_configs` (
        `id` int(11) NOT NULL COMMENT 'id',
        `key` varchar(100) NOT NULL COMMENT 'key',
        `value` text NOT NULL COMMENT 'value',
        `create_time` int(11) NOT NULL COMMENT '创建时间'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='配置表';
    
     */
    namespace CommonService;
    class ConfigService extends CommonService {
        /**
         * 添加一个key
         * $key = 'total_donate'
         * @param $key
         * @param $val
         * @return array
         */
        public function addOneKey($key,$val) {
            // 查询key是否存在
            $configs = M('configs');
            $r = $configs->where(array('key'=>$key))->find();
            if ($r !== false) { // 添加
                $data = [
                    'key'        => $key,
                    'value'      => $val,
                    'create_time'=> time()
                ];
                return $configs->add($data);
            } else { 
                return $configs->where(['key'=>$key])->save(['value'=>$val]);
            }
        }
    
        /**
         * 更新单个key
         * $key = 'total_donate'
         * @param $key
         * @param $val
         * @return array
         */
        public function updateOneKey($key,$val) {
            $configs = M('configs');
            return $configs->where(['key'=>$key])->save(['value'=>$val]);
        }
    
        /**
         * 查询单个key
         * $key = 'total_donate';
         * @param $key
         * @return array
         */
        public function queryOneKey($key) {
            $configs = M('configs');
            $data = $configs->where(['key'=>$key])->find();
            if ($data) {
                return $data['value'];
            } else {
                return false;
            }
        }
    
        /**
         * 查询多个key
         * $keys = 'total_donate,total_help,total_join';
         * $keys = ['total_donate','total_help','total_join'];
         * @param array $keys
         * @return mixed
         */
        public function queryKeys($keys = [])
        {
            $where = [];
            if ($keys) {
                $where['key'] = ['in', $keys];
            }
            $configs = M('configs');
            $data = $configs->where($where)->getField('`key`, `value`');
            return $data;
        }
    
        /**
         * 查询key
         * $key = ['key'=>['in', 'total_donate,total_help,total_join']];
         * $key = ['key'=>['in', ['total_donate','total_help','total_join']]];
         * @param $key
         * @return array
         */
        public function queryKey($key) {
            $configs = M('configs');
            $website = $configs->where($key)->getField('`key`, `value`');
            return $website;
        }
    
    }
    

    更全面了!很实用的配置服务。

  • 相关阅读:
    仿百度排列图片预览插件-Simple Lightbox
    vue2.0移动端自定义性别选择提示框
    微信小程序踩坑记
    网页里如何使用js禁用F12事件
    使用 html2canvas 实现浏览器截图
    h5、jq 移动端评论点攒功能
    js 数字递增特效 仿支付宝我的财富 HTML5
    aos.js超赞页面滚动元素动画jQuery动画库
    简洁AngularJS框架后台管理系统bootstrap后台模板
    Ionic 的 ng-class 在聊天功能上面的巧妙运用
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/12679357.html
Copyright © 2011-2022 走看看