PHP code example of swoft-laravel / database

1. Go to this page and download the library: Download swoft-laravel/database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swoft-laravel / database example snippets


return [
    // 默认连接
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
        'default' => [
            // 数据库驱动, comysql 为异步客户端
            'driver'    => 'comysql',
            'host'      => env('DB_HOST', '127.0.0.1'),
            'port'      => env('DB_PORT', '3306'),
            'database'  => env('DB_DATABASE', 'your database'),
            'username'  => env('DB_USERNAME', 'you user'),
            'password'  => env('DB_PASSWORD', 'your password'),
            'charset'   => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix'    => 'your table prefix',
            'strict'    => true,
            'engine'    => null,
        ],
        'other' => [
            // 数据库驱动, comysql 为异步客户端
            'driver'    => 'comysql',
            'host'      => env('DB_HOST', '127.0.0.1'),
            'port'      => env('DB_PORT', '3306'),
            'database'  => env('DB_DATABASE', 'your database'),
            'username'  => env('DB_USERNAME', 'you user'),
            'password'  => env('DB_PASSWORD', 'your password'),
            'charset'   => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix'    => 'your table prefix',
            'strict'    => true,
            'engine'    => null,
        ],
    ],
    // 连接池
    'pools' => [
        // 别名配置,不可以和 connections中的database重名,否则会被识别为连接池配置
        'default_config'  => [
            // 最大连接数 
            'max_connection' => 100,
            // 启动时创建连接数
            'min_connection' => 20,
            // 查询超时时间
            'max_wait_time'  => 2000
        ],
        // 使用别名方式引用配置
        'default' => 'default_config',
        // 直接使用数组进行连接池配置
        'other'       => [
            'max_connection' => 200,
            'min_connection' => 20,
            'max_wait_time'  => 2000
        ],
        // 开启连接池调试
        'debug_sample'      => [
            'max_connection' => 200,
            'min_connection' => 20,
            'max_wait_time'  => 2000,
            // 需要指定连接池调试信息输出格式
            'debug' => [
               'enable'   => true,
               'tpl'      => "%.4f|co[%3d]|pool[%s]:%8s, stats: [%3d/%3d], usage: [%3d/%3d], waiting: %3d",
               // timestamp |  cid   |  database  |  action  |    current    |    capacity    |     used    |   current      |   waiting
               // 时间戳     | 协程id |  连接池名    |  动作   |  当前活跃连接数 |  连接池最大容量 | 已使用连接数 |  当前可用连接数  |  等待连接数
               'tpl_fields' => ['timestamp', 'cid',  'database',  'action', 'current', 'capacity', 'used',  'current', 'waiting'],
           ],
           // 或者 直接开启或关闭调试, 调试信息采用默认格式
           'debug' => true
        ],
    ]
];

return [
    ...
    'bootScan'     => [
        ...
        'SwoftLaravel\\Database\\Listener',
        ...
    ],
    ...
];

use SwoftLaravel\Database\DatabaseServiceProvider;
class OnWorkerStartListener implements WorkerStartInterface {
    public function onWorkerStart(Server $server, int $workerId, bool $isWorker) {
        if ($isWorker){
            $confPath = BASE_PATH.'/config/database.php';
            DatabaseServiceProvider::init($confPath);
        }
    }
}

 
use SwoftLaravel\Database\Capsule as DB;
use Swoole\Coroutine\Channel;
class Controller {
    public function demo(){
        $user = DB::table('user')
            ->where('name', 'ricky')
            >where('mobile', 13800138000)
            ->get();
    }
    // 需要swoole 4.0.3 以上版本
    // 使用协程并发读数据库
    public function go(){
        $chan =  new Channel(2);
        go(function () use($chan) {
            $code = true;
            $user = DB::connection('ucenter')->table('user')->where('id', 1)->get();
            if ($user == null){
                $code = false;
            }
            $chan->push([$code, 'user', $user]);
        });
        go(function () use($chan){
            $code = true;
            $profile =  DB::connection('ucenter')->table('profile')->where('uid', 1)->get();
            if ($profile == null){
                $code = false;
            }
            $chan->push([$code, 'profile', $profile]);
        });
        $count = 2;
        $success = true;
        $result = [];
        while ($count-- > 0){
            [$code, $field, $result] = $chan->pop();
            $success &= $code;
            $result[$field] = $result;
        }
        
    }
}

//----------------------------------------------
use Illuminate\Database\Eloquent\Model;
class User extends Model {
    protected $table = 'user';
}
class Controller {
    public function demo(){
        $user = User::find(6);
    }
}