• Start

Languages

/

PHP

/

Libraries

/

Surqlize (ORM)

Code generation and CLI

Generate typed field adapters for Surqlize models and run the Surqlize CLI for field generation, schema application, and memory reports.

Typed callbacks work through FieldSet classes. Surqlize infers a model's fields at runtime, and can also generate explicit field adapter classes for better IDE completion and stricter static analysis.

A generated field adapter is a FieldSet subclass that declares each property as a typed field. It makes the closures in select(), where(), and the other builder methods fully analysable.

use App\Models\User;
use Surqlize\Query\Fields\FieldSet;
use Surqlize\Query\Fields\NumericField;
use Surqlize\Query\Fields\RecordIdField;
use Surqlize\Query\Fields\RecordLinkField;
use Surqlize\Query\Fields\StringField;

final class UserFields extends FieldSet
{
public readonly RecordIdField $id;
public readonly StringField $name;
public readonly NumericField $age;
public readonly RecordLinkField $address;

public function __construct()
{
parent::__construct(User::class);

$this->id = new RecordIdField('id', table: 'user');
$this->name = new StringField('name');
$this->age = new NumericField('age');
$this->address = new RecordLinkField('address');
}
}

You write these by hand only if you want to. The generator produces them for you.

The CLI reads a config file, by convention surqlize.config.php, that returns an array. For field generation it lists the models and where to write the adapters.

use App\Models\Address;
use App\Models\HasAddress;
use App\Models\User;

return [
'models' => [
User::class,
Address::class,
HasAddress::class,
],
'fields_namespace' => 'App\\Models\\Fields',
'fields_path' => __DIR__ . '/src/Models/Fields',
];

For schema:apply, the config provides the models plus an SDK executor.

return [
'models' => [User::class, Address::class],
'executor' => $surreal,
];

Surqlize ships a Composer binary named surqlize.

vendor/bin/surqlize generate:fields [config-path]
vendor/bin/surqlize schema:apply [config-path]
vendor/bin/surqlize memory:footprint [--iterations=1000] [--output=path]

In a source checkout without Composer's bin proxy, run the binary directly with php bin/surqlize ....

Generates the *Fields classes and *FieldTyping traits for the configured models, into the configured namespace and path.

vendor/bin/surqlize generate:fields surqlize.config.php

Applies the schema definitions for the configured models through the configured executor.

vendor/bin/surqlize schema:apply surqlize.config.php

Generates a JSON memory report across the built-in scenarios (metadata reflection, field-set resolution, query compilation, hydration, graph traversal, and more), with retained, peak, and real memory deltas and durations.

vendor/bin/surqlize memory:footprint --iterations=5000 --output=memory-report.json

Was this page helpful?