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 简单数据模型 元数据和应用数据分离 弱一致性 优势: 避免不必要的复 ...
随机推荐
- MarkDown和流程图诠释你的代码
写在前面:首先感谢导师-猴哥对我的认可(求多分享点编程经验.工具.多开课),学习编程是一个痛苦和快乐的过程,希望大家共勉 本文介绍MarkDown的基本语法.使用MarkDown画简单的流程图.使用X ...
- oracle查询时间
oracle查询和时间有关的命令: 方法一:select * from dual where time between to_date('2012-06-18 00:00:00','yyyy-mm-d ...
- laravel5.4学习笔记
1.安装laravel可以直接用composer安装,然后用laravel new xxx来新建项目 服务器上安装了composer(php包管理工具)以后, composer global requ ...
- js常用函数汇总(不定期更新)
1.图片按比例压缩 function setImgSize(){ var outbox_w=imgbox.width(), outbox_h=imgbox.height(); imgbox.find( ...
- Android(java)学习笔记53:局部内部类
1. 局部内部类 /* 局部内部类 A:可以直接访问外部类的成员 B:在局部位置,可以创建内部类对象,通过对象调用内部类方法,来使用局部内部类功能 面试题: 局部内部类访问局部变量的注意事项? A:局 ...
- LA 4254 贪心
题意:有 n 个工作,他的允许的工作时间是 [l,r] ,工作量是 v ,求CPU最速度的最小值. 分析: 可能太久没有做题了,竟然脑子反应好慢的.还是很容易想到二分,但是二分怎么转移呢? 可以看出, ...
- 【iOS】那些年,遇到的小坑
'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjectsAndKeys:]: second ...
- darknet53 yolo 下的识别训练
[目录] 一. 安装Darknet(仅CPU下) 2 1.1在CPU下安装Darknet方式 2 1.2在GPU下安装Darknet方式 4 二. YOLO.V3训练官网数据集(VOC数据集/COCO ...
- 【洛谷P1879】玉米田Corn Fields
玉米田Corn Fields 题目链接 此题和互不侵犯状压DP的做法类似 f[i][j]表示前i行,第i行种植(1)/不种植(0)构成的二进制数为j时的方案数 首先我们可以预处理出所有一行中没有两个相 ...
- 学大伟业 Day 5 培训总结
今天讲数据结构 先从mzx大佬的ppt摘抄一段: 数据结构是计算机存储.组织数据的方式.数据结构是指相互之间存在一种或多种特定关系的数据元素的集合. 通常情况下,精心选择的数据结构可以带来更高的运行或 ...