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 简单数据模型 元数据和应用数据分离 弱一致性 优势: 避免不必要的复 ...
随机推荐
- 尝试VS插件
从试用vs2013开始,ide变得越来越智能,但是vs2013总是会出一些莫名其妙的问题,导致编译不成功,不能跟vs2010共享等等.于是由再次回到vs2010. 现在vs2015的update1更新 ...
- CSS中的各类选择器
属性选择器 结构性伪类选择器 UI状态伪类选择器 CSS其他选择器
- matlab练习程序(Hilbert图像置乱)
正好刚写了Hibert生成曲线,不如再加一篇应用的程序. 关于Hilbert图像置乱,我在网上搜的应用领域主要集中在数字水印和图像加密上,而这两个领域我都没怎么接触过. 大部分的图像置乱都是如下图的置 ...
- 搭建JUnit环境
1.下载 JUnit,这里用JUnit 4.7 下载链接: http://pan.baidu.com/s/1c23n7LQ 密码: i18e 2.可以直接 build path 引入:也可以创建 Us ...
- java面试之----堆(heap)、栈(stack)和方法区(method)
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method)也叫静态存储区. 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的 ...
- OLAP和数据挖掘的区别
总结来说: 数据仓库提供了一个分析的数据源 数据挖掘能分析出未知的信息,提出假设 OLAP能通过分析,验证假设 从技术角度看,商务智能的过程是企业的决策人员以企业中的数据仓库为基础,经由数据挖掘工具. ...
- Hadoop ->> 关于数据分割(Data Split)的思考
今天开始学习Hadoop这门热门的数据库技术.直接从被奉为Hadoop圣经的<Hadoop The Definitive Guide 4th Edition>入手.第一章作者写到一个关于分 ...
- 用AutoHotkey重置Excel的Ctrl+Alt+方向键选择的范围
当前选择是蓝色单元格,原来的Ctrl+Alt+方向键,是选中[蓝色]到[红色]单元格的区域, 经过脚本修改后,则是[蓝色]到[黄色]的区域,应该更符合日常使用, 特别是表格比较大的时候,一直按住鼠标滚 ...
- Java传引用问题
Java传引用问题 使用Java调用方法时,可以传值,也可以传引用.下面说说两者的区别: 1.传值 传值中的"值"类型是指java的8大基本类型(基础知识,不知道 ...
- 高效实时的网络会议数据传输库—UDT
在视频会议系统的研发当中,我们的音.视频数据必须要有相应的可靠性作为保障,因为视频会议系统是一个实时性非常强的系统,如果其数据在网络不太好的情况下,有可能会出现丢包.数据延迟.数据堵塞等现象,出现这些 ...