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 简单数据模型 元数据和应用数据分离 弱一致性 优势: 避免不必要的复 ...
随机推荐
- Android 自定义倾斜字体
public class RotateTextView extends AppCompatTextView { private static final int DEFAULT_DEGREES = 0 ...
- greenplum维护
1.用户管理 psql -d sea CREATE DATABASE BI; CREATE USER ubi WITH PASSWORD 'pwdbi' NOSUPERUSER; GRANT ALL ...
- 并发包java.util.concurrent.CountDownLatch
/** * * @描述: 倒计时器 . * 犹如倒计时计数器,调用CountDownLatch对象的countDown方法就将计数器减1,当计算器为0的时候 * 则所有等待者或单个等待者开始执行 * ...
- day009-IO流
什么叫流?就是数据的流动.以内存为基准,分为输入input和输出output.输入也叫做读取数据,输出也叫写出数据. 分类 按数据的流向分: 输入流.输出流 按数据类型分: 字节流.字符流 1. ...
- SpringMvc-view
1.view视图:及springmvc返回到前端的页面,前面的所有跳转都是view的列子在此就不在举例了 2.在view界面中如何实现国际化? 2.1实现国际化有首先需要配置国际化资源文件:例如 英文 ...
- 【Spring开发】—— AOP之方法级拦截
前言: 前面介绍了Spring的核心模块以及相关的依赖注入等概念.这篇讲解一下spring的另一个重点,AOP面向切面编程. 说道AOP不得不提到几个概念: 切面:也就是我们自己的一些业务方法. 通知 ...
- June 06th 2017 Week 23rd Tuesday
At the touch of love, everyone becomes a poet. 一谈到爱,每个人都变成了一位诗人. Sweet words always have the power o ...
- SQL server 2008 安装报错 reporting services catalog database file existence
SQL server 2008 安装时报错 提示 reporting services catalog database file existence 查了一下,是因为原来装过Sql server 2 ...
- Python的基本库与第三方库
一:Python 模块,包,库的概念理解: 1.python模块是: python模块:包含并且有组织的代码片段为模块. 表现形式为:写的代码保存为文件.这个文件就是一个模块.sample.py 其中 ...
- scope的四种作用域的使用
如何使用spring的作用域: <bean id="role" class="spring.chapter2.maryGame.Role" scope=& ...