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/ */
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);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.