// 连接的其他数据库
$connection = new \yii\db\Connection([
'dsn' => 'mysql:host=10.10.2.171;dbname=databasename',
'username' => 'admin',
'password' => '1234567',
'charset' => 'utf8',
]);
$connection->open();
// 筛选条件
$where = 'where 1=1' ;
$where = $user_id ? $where.' and UserID = '.$user_id :$where;
$where = $ObjectType ? $where.' and ObjectType = '.$ObjectType :$where;
$where = $type ? $where.' and SubType = '.$type : $where;
if($start && $end){
$where = $where." and '".$start." 00:00:00 '"." <= CreateTime and CreateTime <= '".$end." 23:59:59 '";
}
if($ObjectID){
$infos_quary = $connection->createCommand("SELECT CameraID FROM CameraInfo where CameraName like '%".$ObjectID."%'");
$infos = $infos_quary->queryAll();
$ids = [];
foreach ($infos as $key=>$value){
$ids[] =$value['CameraID'];
}
if($ids){
$where = $where.' and ObjectID in ('.implode(",",$ids).')';
}
}
// 总数:分页使用
$commandall = $connection->createCommand('SELECT * FROM OperationLog '.$where);
$countriesall = $commandall->queryAll();
// 拼接查询语句
$command = $connection->createCommand('SELECT * FROM OperationLog '.$where.' ORDER BY OperationID DESC limit '.$page.','.$page_size);
$countries = $command->queryAll();
YII 接口内连接其他数据库方法
于 2023-04-06 15:58:42 首次发布