YII2 多MongoDB配置和使用
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库
<?php namespace app\models; use yii\mongodb\ActiveRecord;
//类名 对应到数据表名称
class SysOperateLog extends ActiveRecord
{
public static function add($controllerId, $actionId, $getParams, $postParams, $userId, $userName, $league_id, $league_name, $remoteAddr, $httpUserAgent, $createDatetime)
{
$log = new SysOperateLog();
$log->_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重新构建的新的库
<?php namespace app\models; use app\librarys\Tools;
use yii\mongodb\ActiveRecord; class ErpSysOperateLog extends ActiveRecord
{
//重写类名 将原有的 ErpSysOperateLog类转换成 SysOperateLog
// public static function collectionName()
// {
// return 'sys_operate_log';
// } //配置选择第二个MongoDB 数据库 下面的查询方法 添加方法都是一样的
public static function getDb()
{
return \Yii::$app->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);
YII2 多MongoDB配置和使用的更多相关文章
- YII2操作mongodb笔记(转)
componets配置: 'mongodb' => [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://test: ...
- yii2的urlManager配置
网址伪静态是一个非常常用的网站需求,Yii2可以非常简单地进行配置. 首先,在配置文件config/main.php的'components' 段中,加入如下设置:'urlManager'=>a ...
- hadoop生态搭建(3节点)-13.mongodb配置
# 13.mongodb配置_副本集_认证授权# ==================================================================安装 mongod ...
- Yii2 的快速配置 api 服务 yii2-fast-api
yii2-fast-api yii2-fast-api是一个Yii2框架的扩展,用于配置完善Yii2,以实现api的快速开发. 此扩展默认的场景是APP的后端接口开发,因此偏向于实用主义,并未完全采用 ...
- Yii2中mongodb使用ActiveRecord的数据操作
概况 Yii2 一个高效安全的高性能PHP框架.mongodb 一个高性能分布式文档存储NOSQL数据库. 关于mongodb与mysql的优缺点,应该都了解过. mysql传统关系数据库,安全稳定 ...
- centos中docker mongodb 配置
安装docker,对于Centos7,如下: $ sudo yum update$ sudo yum -y install docker$ sudo systemctl start docker 首先 ...
- 把Mongodb配置成windows服务
在mongodb/bin 下运行命令窗口需要配置日志和db路径,如下:mongod --logpath d:\mongo\logs\logfilename.log --logappend --dbpa ...
- spring data mongodb 配置遇到的几个问题
一. mongodb 2.2版本以上的配置 spring.data.mongodb.uri = mongodb://newlook:newlook@192.168.0.109:27017/admin ...
- mongodb配置
Mongodb1. 安装2. CRUD3. 索引4. 副本及(replica sets)5. 分片(sharding) nosql 简单数据模型 元数据和应用数据分离 弱一致性 优势: 避免不必要的复 ...
随机推荐
- Python学习系列----第五章 模块
5.1 如何引入模块 在Python中用关键字import来引入某个模块,比如要引用模块math,就可以在文件最开始的地方用import math来引入.在调用math模块中的函数时,必须这样引用: ...
- css Media Query详解
Media Queries详解 Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: 1 <link href="css/re ...
- Vue2自定义指令改变DOM值后未刷新data中绑定属性的值
标签(空格分隔): Vue 自定义指令用于过滤输入框,只允许输入数字: Vue.directive('numberOnly', { bind: function (el, binding) { el. ...
- JSP中include动作与指令
include指令 JSP中有三大指令:page,include,taglib,之前已经说过了page的用法.这里介绍下include. 使用语法如下: <%@ include file=&qu ...
- 如何为属性是disabled的表单绑定js事件
$(document).click(function(e){ var el = e.target; if (el.tagName == 'INPUT') { $(el).removeAttr('dis ...
- Linux 网卡的解决方法
1. 编辑70-persistent-net配置文件: # vi /etc/udev/rules.d/70-persistent-net.rules 如果没有就新建一个,添加如下内容: # PCI d ...
- python接口测试-项目实践(一) 测试需求与测试思路
测试需求: 第三方系统提供了3个接口,需要测试前端显示的字符串里的对应数据与接口数据是否一致. 测试分层: 开发人员的设计:每周从接口取一次数据,拼接完成后保存到数据库.再从数据库取数提供接口给前端开 ...
- Android(java)学习笔记35:如何改变Spinner系统自带的字体和颜色
1. 首先我们要知道Spinner系统自带字体和颜色本质: 原生的Spring 控件是无法更改字体和颜色的... 从下面的代码可以看出...红色的标注显示使用的是Android默认的布局.. Spin ...
- 【转】学习jar命令 创建和解压jar文件包
java编程中每天都用不少jar文件,项目开发中不停地导包,在忙完了一天的工作,放下那些复杂的业务实现,不仅想问这些jar包怎么生成的,jar包有哪些独特的地方等等. 原来这些经常见到的jar包是ja ...
- MySQL优化 ----开篇
今天,数据库的操作越来越成为整个应用的性能瓶颈,Mysql优化则是一个经常要谈的问题了. 谈起MySQL优化,咱们先简单谈一下Mysql: Mysql是最流行的关系型数据库管理系统,在WEB应用方面M ...