Medoo Database
webman/medoo adds a connection pool feature based on Medoo and supports both coroutine and non-coroutine environments, with usage identical to Medoo.
Note
This manual is for webman v2. If you are using webman v1, please refer to the v1 manual
Installation
composer require webman/medoo
Medoo Database Configuration
The configuration file is located at config/plugin/webman/medoo/database.php
Using Medoo Database
<?php
namespace app\controller;
use support\Request;
use support\Medoo;
class Index
{
public function index(Request $request)
{
$user = Medoo::get('user', '*', ['uid' => 1]);
return json($user);
}
}
Tip
Medoo::get('user', '*', ['uid' => 1]);
is equivalent to
Medoo::instance('default')->get('user', '*', ['uid' => 1]);
Medoo Multi-Database Configuration
Configuration
Add a new configuration in config/plugin/webman/medoo/database.php
, with any key; here it is other
.
<?php
return [
'default' => [
'type' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
'prefix' => '',
'logging' => false,
'error' => PDO::ERRMODE_EXCEPTION,
'option' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL
],
'command' => [
'SET SQL_MODE=ANSI_QUOTES'
],
'pool' => [ // Connection pool configuration
'max_connections' => 5, // Maximum connections
'min_connections' => 1, // Minimum connections
'wait_timeout' => 60, // Maximum wait time for acquiring a connection from the pool; throws an exception on timeout
'idle_timeout' => 3, // Maximum idle time for connections in the pool; will close and recycle after timeout, until the number of connections is min_connections
'heartbeat_interval' => 50, // Connection pool heartbeat detection interval, in seconds; suggested to be less than 60 seconds
]
],
// Here is a new configuration called other
'other' => [
'type' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
'prefix' => '',
'logging' => false,
'error' => PDO::ERRMODE_EXCEPTION,
'option' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL
],
'command' => [
'SET SQL_MODE=ANSI_QUOTES'
],
'pool' => [
'max_connections' => 5,
'min_connections' => 1,
'wait_timeout' => 60,
'idle_timeout' => 3,
'heartbeat_interval' => 50,
],
],
];
Using Medoo Database
$user = Medoo::instance('other')->get('user', '*', ['uid' => 1]);
See the official Medoo documentation