PHP code example of sad_spirit / pg_gateway

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

    

sad_spirit / pg_gateway example snippets


use sad_spirit\pg_gateway\{
    TableLocator,
    builders\FluentBuilder
};
use sad_spirit\pg_wrapper\Connection;

$connection = new Connection('...');
$locator    = new TableLocator($connection);

$adminRoles = $locator->createGateway('example.roles')
    ->select(fn(FluentBuilder $builder) => $builder
        ->operatorCondition('name', '~*', 'admin')
        ->outputColumns()
            ->except(['description'])
            ->replace('/^/', 'role_'));

$activeAdminRoles = $locator->createGateway('example.users_roles')
    ->select(fn(FluentBuilder $builder) => $builder
        ->sqlCondition("current_date between coalesce(self.valid_from, 'yesterday') and coalesce(self.valid_to, 'tomorrow')")
        ->join($adminRoles)
            ->onForeignKey()
        ->outputColumns()
            ->only(['valid_from', 'valid_to']));

$activeAdminUsers = $locator->createGateway('example.users')
    ->select(fn(FluentBuilder $builder) => $builder
        ->outputColumns()
            ->except(['password_hash'])
            ->replace('/^/', 'user_')
        ->join($activeAdminRoles)
            ->onForeignKey()
        ->orderBy('user_login, role_name')
        ->limit(5));

// Let's assume we want to output that list with pagination
echo "Total users with active admin roles: " . $activeAdminUsers->executeCount() . "\n\n";

foreach ($activeAdminUsers as $row) {
    print_r($row);
}

echo $activeAdminUsers->createSelectCountStatement()->getSql() . ";\n\n";
echo $activeAdminUsers->createSelectStatement()->getSql() . ';';