PHP code example of swoft-laravel-x1024 / database2
1. Go to this page and download the library: Download swoft-laravel-x1024/database2 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 Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Log\Helper\CLog;
use Swoft\Server\SwooleEvent;
use SwoftLaravel\Database\DatabaseServiceProvider;
/**
* Class WorkerStartListener
*
* @since 2.0
*
* @Listener(event=SwooleEvent::WORKER_START)
*/
class WorkerStartListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*/
public function handle(EventInterface $event): void
{
$confPath = __DIR__ . '/../../config/database.php';
DatabaseServiceProvider::init($confPath);
CLog::debug($confPath);
}
}
namespace App;
use Doctrine\Common\Annotations\AnnotationReader;
use Swoft\Annotation\AnnotationRegister;
use Swoft\SwoftApplication;
use function date_default_timezone_set;
/**
* Class Application
*
* @since 2.0
*/
class Application extends SwoftApplication
{
protected function beforeInit(): void
{
parent::beforeInit();
//忽略异常注释
AnnotationReader::addGlobalIgnoredName('mixin');
date_default_timezone_set('Asia/Shanghai');
}
}
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.