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);
