博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
YII2 多MongoDB配置和使用
阅读量:6413 次
发布时间:2019-06-23

本文共 9311 字,大约阅读时间需要 31 分钟。

1:在config/web.php 文件下配置多个连接即可:

注意在componets 下

'mongodb' => [            'class' => '\yii\mongodb\Connection',            'dsn' => 'mongodb://192.168.20.201:27017/boss-test',        ],        'mongodb_erpmall' => [            'class' => '\yii\mongodb\Connection',            'dsn' => 'mongodb://192.168.20.201:27017/erpmall-test',        ],

  对应的两个不同的数据库

   

2创建MongoDB的model文件

2.1 原本 web.php 使用的 MongoDB库

_id = Tools::uuid(); $log->controllerId = $controllerId; $log->actionId = $actionId; $log->getParams = $getParams; $log->postParams = $postParams; $log->userId = $userId; $log->userName = $userName; $log->league_id = $league_id; $log->league_name = $league_name; $log->remoteAddr = $remoteAddr; $log->httpUserAgent = $httpUserAgent; $log->createDatetime = $createDatetime; $log->durationTime = null; $log->exceptionCode = null; $log->exceptionMessage = null; $log->exceptionTraceMessage = null; $result = $log->save(); if ($result == true) { return $log->_id; } else { return null; } } public static function setDurationTime($id, $durationTime) { $log = self::find()->where(['_id' => $id])->one(); $log->durationTime = $durationTime; $log->update(); } public static function getById($id) { $log = self::find()->where(['_id' => $id])->one(); return $log; } public static function getList($page, $pageSize, $controllerId, $actionId, $durationTime, $startTime, $endTime) { $whereParams = []; if (!empty($controllerId)) { $whereParams['controllerId'] = $controllerId; } if (!empty($actionId)) { $whereParams['actionId'] = $actionId; } $items = self::find()->where($whereParams); if (!empty($durationTime)) { if (!empty($whereParams)) { $items->andWhere(['>=', 'durationTime', intval($durationTime)]); } else { $items->where(['>=', 'durationTime', intval($durationTime)]); } } if (!empty($startTime)) { $stime = strtotime($startTime); if (!empty($whereParams) || !empty($durationTime)) { $items->andWhere(['>=', 'createDatetime', $stime]); } else { $items->Where(['>=', 'createDatetime', $stime]); } } if (!empty($endTime)) { $etime = strtotime($endTime); if (!empty($whereParams) || !empty($durationTime) || !empty($startTime)) { $items->andWhere(['<', 'createDatetime', $etime]); } else { $items->where(['<', 'createDatetime', $etime]); } } return $items->offset($page * $pageSize) ->limit($pageSize) ->orderBy('createDatetime desc') ->asArray() ->all(); } public static function getCount($controllerId, $actionId, $durationTime, $startTime, $endTime) { $whereParams = []; if (!empty($controllerId)) { $whereParams['controllerId'] = $controllerId; } if (!empty($actionId)) { $whereParams['actionId'] = $actionId; } $items = self::find()->where($whereParams); if (!empty($durationTime)) { if (!empty($whereParams)) { $items->andWhere(['>=', 'durationTime', intval($durationTime)]); } else { $items->where(['>=', 'durationTime', intval($durationTime)]); } } if (!empty($startTime)) { $stime = strtotime($startTime); if (!empty($whereParams) || !empty($durationTime)) { $items->andWhere(['>=', 'createDatetime', $stime]); } else { $items->Where(['>=', 'createDatetime', $stime]); } } if (!empty($endTime)) { $etime = strtotime($endTime); if (!empty($whereParams) || !empty($durationTime) || !empty($startTime)) { $items->andWhere(['<', 'createDatetime', $etime]); } else { $items->where(['<', 'createDatetime', $etime]); } } return $items->count(); } public function attributes() { return [ '_id', // pk 前台操作日志 'controllerId', // 请求的 controller id 'actionId', // 请求的 action id 'getParams', // 请求的get参数数组 'postParams', // 请求的post参数数组 'userId', // 用户id 'userName', // 用户姓名 'league_id', //加盟商id 'league_name', //加盟商名称 'remoteAddr', // 访问的来源地址ip 'httpUserAgent', // 访问者的浏览器标识 'createDatetime', // 请求时间 'durationTime', // 请求持续时间(毫秒) 'exceptionCode', 'exceptionMessage', 'exceptionTraceMessage' ]; }}

 2.2重新构建的新的库

get('mongodb_erpmall'); } public static function getById($id) { $log = self::find()->where(['_id' => $id])->one(); return $log; } public static function getList($page, $pageSize, $current_operate_type, $controllerId, $actionId, $durationTime, $startTime, $endTime) { $whereParams = []; if ($current_operate_type != -1) { $whereParams['current_operate_type'] = (int)$current_operate_type; } if (!empty($controllerId)) { $whereParams['controllerId'] = $controllerId; } if (!empty($actionId)) { $whereParams['actionId'] = $actionId; } $items = self::find()->where($whereParams); if (!empty($durationTime)) { if (!empty($whereParams)) { $items->andWhere(['>=', 'durationTime', intval($durationTime)]); } else { $items->where(['>=', 'durationTime', intval($durationTime)]); } } if (!empty($startTime)) { $stime = strtotime($startTime); if (!empty($whereParams) || !empty($durationTime)) { $items->andWhere(['>=', 'createDatetime', $stime]); } else { $items->Where(['>=', 'createDatetime', $stime]); } } if (!empty($endTime)) { $etime = strtotime($endTime); if (!empty($whereParams) || !empty($durationTime) || !empty($startTime)) { $items->andWhere(['<', 'createDatetime', $etime]); } else { $items->where(['<', 'createDatetime', $etime]); } } return $items->offset($page * $pageSize) ->limit($pageSize) ->orderBy('createDatetime desc') ->asArray() ->all(); } public static function getCount($current_operate_type, $controllerId, $actionId, $durationTime, $startTime, $endTime) { $whereParams = []; if ($current_operate_type != -1) { $whereParams['current_operate_type'] = (int)$current_operate_type; } if (!empty($controllerId)) { $whereParams['controllerId'] = $controllerId; } if (!empty($actionId)) { $whereParams['actionId'] = $actionId; } $items = self::find()->where($whereParams); if (!empty($durationTime)) { if (!empty($whereParams)) { $items->andWhere(['>=', 'durationTime', intval($durationTime)]); } else { $items->where(['>=', 'durationTime', intval($durationTime)]); } } if (!empty($startTime)) { $stime = strtotime($startTime); if (!empty($whereParams) || !empty($durationTime)) { $items->andWhere(['>=', 'createDatetime', $stime]); } else { $items->Where(['>=', 'createDatetime', $stime]); } } if (!empty($endTime)) { $etime = strtotime($endTime); if (!empty($whereParams) || !empty($durationTime) || !empty($startTime)) { $items->andWhere(['<', 'createDatetime', $etime]); } else { $items->where(['<', 'createDatetime', $etime]); } } return $items->count(); } public function attributes() { return [ '_id', // pk 前台操作日志 'current_operate_type', //0 微信端 1 pc端 2 员工端 'controllerId', // 请求的 controller id 'actionId', // 请求的 action id 'getParams', // 请求的get参数数组 'postParams', // 请求的post参数数组 'userId', // 用户id 'userName', // 用户姓名 'league_id', //加盟商id 'league_name', //加盟商名称 'remoteAddr', // 访问的来源地址ip 'httpUserAgent', // 访问者的浏览器标识 'createDatetime', // 请求时间 'durationTime', // 请求持续时间(毫秒) 'exceptionCode', 'exceptionMessage', 'exceptionTraceMessage' ]; }}

 3 控制器的调用完全是一模一样 

   3.1默认配置

$records = SysOperateLog::getList($pagination->page, $pagination->pageSize, $controllerId, $actionId, $durationTime, $startTime, $endTime);

  3.2其他的配置

$records = ErpSysOperateLog::getList($pagination->page, $pagination->pageSize, $current_operate_type, $controllerId, $actionId, $durationTime, $startTime, $endTime);

转载于:https://www.cnblogs.com/lt-com/p/8204491.html

你可能感兴趣的文章
Android onMeasure方法介绍
查看>>
微信公众号搭建营销型房产项目程序后台开发
查看>>
git使用笔记
查看>>
无锁数据结构
查看>>
RabbitMQ消息队列:任务分发机制
查看>>
substr和substring的区别
查看>>
String.Format用法
查看>>
【转】java NiO 学习笔记
查看>>
MySQL的变量查看和设置
查看>>
Android NDK配、编译、调试
查看>>
长平狐 memcached源代码阅读笔记(二):网络处理部分
查看>>
android onNewIntent
查看>>
实战利用腾讯企业邮箱zabbix3.x邮件(微信/QQ/短信)告警详细配置
查看>>
干掉运营商:神奇盒子让你自建GSM 网络
查看>>
配置企业级wlan
查看>>
XML特殊符号
查看>>
kaptcha可配置项
查看>>
JavaMail邮箱验证用户注册
查看>>
系统时间——ntpd
查看>>
反射实现AOP动态代理模式(Spring AOP实现原理)
查看>>