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