PHP code example of reactphp-x / orm

1. Go to this page and download the library: Download reactphp-x/orm 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/ */

    

reactphp-x / orm example snippets



DB::enableQueryLog();

$promises = [];

for ($i=0; $i < 10; $i++) { 
    $a = \React\Async\async(fn() => User::with('posts.comments', 'posts.tags')->get()->each(function ($user) {
        echo $user->name . PHP_EOL;
        $user->posts->each(function ($post) {
            echo $post->title . PHP_EOL;
            $post->comments->each(function ($comment) {
                echo $comment->content . PHP_EOL;
            });
        });
    }))();

    $promises[] = $a;
    
    
    $b = \React\Async\async(fn() => Post::with('user', 'comments', 'tags')->get()->each(function ($post) {
        echo $post->title . PHP_EOL;
        echo $post->user->name . PHP_EOL;
        $post->comments->each(function ($comment) {
            echo $comment->content . PHP_EOL;
        });
        $post->tags->each(function ($tag) {
            echo $tag->name . PHP_EOL;
        });
    }))();

    $promises[] = $b;
    
}

\React\Promise\all($promises)->then(function () {
    print_r(DB::getQueryLog());
});




use ReactphpX\Orm\AsyncMysqlConnector;
use ReactphpX\Orm\AsyncMysqlConnection;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Connection;

Connection::resolverFor('async-mysql', function ($connection, $database, $prefix, $config) {
    return new AsyncMysqlConnection($connection, $database, $prefix, $config);
});
$container = new Container;
$container->bind(AsyncMysqlConnector::class, fn () => new AsyncMysqlConnector);
$container->alias(AsyncMysqlConnector::class, 'db.connector.async-mysql'); 

$db = new DB($container);

$db->addConnection([
    'driver' => 'async-mysql',
    'host' => getenv('MYSQL_HOST') ?: '',
    'port' => getenv('MYSQL_PORT') ?: '',
    'database' => getenv('MYSQL_DATABASE') ?: '',
    'username' => getenv('MYSQL_USER') ?: '',
    'password' => getenv('MYSQL_PASSWORD') ?: '',
    'charset' => 'utf8mb4',
    'pool' => [
        'min_connections' => 2, // min 2 connection
        'max_connections' => 10, // max 10 connection
        'max_wait_queue' => 110, // how many sql in queue
        'wait_timeout' => 5,// wait time