PHP code example of mongoose-studio / phobos-framework-database-postgres

1. Go to this page and download the library: Download mongoose-studio/phobos-framework-database-postgres 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/ */

    

mongoose-studio / phobos-framework-database-postgres example snippets




return [
    'default' => 'pgsql',

    'drivers' => [
        'pgsql' => PhobosFramework\Database\Drivers\Postgres\PostgresDriver::class,
    ],

    'connections' => [
        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', 5432),
            'database' => env('DB_DATABASE', 'app'),
            'username' => env('DB_USERNAME', 'postgres'),
            'password' => env('DB_PASSWORD', ''),
            'schema' => 'public',        // Opcional: search_path
        ],
    ],
];

// config: 'search_path' => 'app,public'

class User extends TableEntity {
    protected static ?string $schema = null;  // resuelto por search_path
    protected static string $entity = 'users';
}

class Invoice extends TableEntity {
    protected static ?string $schema = 'ventas';
    protected static string $entity = 'invoices';   // -> "ventas"."invoices"
}

use PhobosFramework\Database\Schema\SchemaRegistry;

// En un middleware, según el tenant del request:
SchemaRegistry::getInstance()->register('ventas', "tenant_{$tenantId}");

// Ahora toda entidad con $schema = 'ventas' escribe/lee en "tenant_42"."invoices"

// 1. auto (por defecto): SERIAL/IDENTITY; el id se lee con INSERT ... RETURNING
class Account extends TableEntity {
    protected static array $pk = ['id'];
    protected static string $keyStrategy = 'auto';
    public ?int $id = null;
}

// 2. uuidv7: el framework genera un UUIDv7 en PHP antes del INSERT (sin round-trip)
class Document extends TableEntity {
    protected static array $pk = ['id'];
    protected static string $keyStrategy = 'uuidv7';
    public mixed $id = null;   // columna UUID
}

// 3. manual: la aplicación asigna el valor
class Setting extends TableEntity {
    protected static array $pk = ['key'];
    protected static string $keyStrategy = 'manual';
    public string $key = '';
}

class Account extends TableEntity {
    protected static string $entity = 'accounts';
    protected static array $casts = [
        'meta'       => 'json',      // JSONB  <-> array PHP
        'active'     => 'bool',      // boolean (entiende 't'/'f')
        'created_at' => 'datetime',  // timestamp <-> DateTimeImmutable
    ];

    public mixed $meta = null;
    public mixed $active = null;
}

$account = new Account();
$account->meta = ['plan' => 'pro', 'seats' => 5];
$account->save();                       // se persiste como JSONB

$found = Account::findByPk($account->id);
$found->meta['plan'];                   // 'pro' (array PHP)

$driver->getSetIsolationLevelSQL('SERIALIZABLE');
// SET TRANSACTION ISOLATION LEVEL SERIALIZABLE