๐ Schema
Note: While the Schema component is part of the Hector ORM ecosystem, it is available as a standalone
package: hectororm/schema.
You can find it on Packagist.
You can use it independently of the ORM, in any PHP application. ๐
Hector Schema is the schema introspection and metadata module of Hector ORM. It provides a simple and consistent API to explore and manage your database structure. While tightly integrated with Hector ORM, this package is entirely independent and can be used on its own.
DBMS compatibility
| DBMS | Version | Compatibility |
|---|---|---|
| MySQL | 5.7 - 9.6 | โ |
| MariaDB | 10.5 - 12.2 | โ |
| Vitess | - | โ |
| SQLite | 3.x | โ |
Note: Versions listed are actively tested. Older versions may work but are not officially supported. Know of a DBMS version not listed here but works fine? Contributions are welcome โ open a PR!
Usage
Generate a schema
The schema generator reads your DB structure and returns rich PHP objects that describe it:
use Hector\Connection\Connection;
use Hector\Schema\Generator\MySQL;
use Hector\Schema\Generator\Sqlite;
// MySQL / MariaDB
$connection = new Connection('mysql:host=localhost;dbname=mydb', 'user', 'pass');
$generator = new MySQL($connection);
// SQLite
$connection = new Connection('sqlite:/path/to/database.db');
$generator = new Sqlite($connection);
$schema = $generator->generateSchema('schema_name');
// Returns a `Hector\Schema\Schema` object
$container = $generator->generateSchemas('schema1_name', 'schema2_name');
// Returns a `Hector\Schema\SchemaContainer` object
Available generators:
Hector\Schema\Generator\MySQLโ for MySQL and MariaDBHector\Schema\Generator\Sqliteโ for SQLite
Tip: You can use different generators for different environments (e.g., dev SQLite, prod MySQL).
Caching schemas
Schema generation can be expensive for large databases. This library does not include built-in caching, as caching strategies vary widely.
Instead, you can serialize schema objects and restore them later:
$serialized = serialize($schema);
file_put_contents('cache/schema.ser', $serialized);
// Later...
$schema = unserialize(file_get_contents('cache/schema.ser'));
Inheritance and references between objects are preserved during (de)serialization. This makes caching simple and flexible โ ideal for integrating into your own caching logic ๐
Warning: Ensure your cache is invalidated when the database schema changes (e.g., after migrations).
API reference
This section covers the main classes used to explore database schemas.
Hector\Schema\SchemaContainer
A container for multiple schemas. Useful when your application interacts with multiple databases or schemas.
Key methods:
getSchemas(?string $connection = null): GeneratorhasSchema(string $name, ?string $connection = null): boolgetSchema(string $name, ?string $connection = null): SchemahasTable(string $name, ?string $schemaName = null, ?string $connection = null): boolgetTable(string $name, ?string $schemaName = null, ?string $connection = null): Table
// Get table from default schema
$usersTable = $container->getTable('users');
// Get table from specific schema
$usersTable = $container->getTable('users', 'my_database');
// Get table from specific schema and connection
$usersTable = $container->getTable('users', 'my_database', 'replica');
This class is iterable:
foreach ($container as $schema)will yieldHector\Schema\Schemaobjects.
Hector\Schema\Schema
Represents a single schema (i.e., database).
Key methods:
getName(bool $quoted = false): stringgetCharset(): stringgetCollation(): stringgetTables(): GeneratorhasTable(string $name): boolgetTable(string $name): TablegetContainer(): ?SchemaContainer
Deprecated: The
$quotedparameter ongetName()is deprecated. UseHector\Query\Statement\Quotedfor driver-aware identifier quoting.
This class is also iterable:
foreach ($schema as $table)yieldsHector\Schema\Tableobjects.
Hector\Schema\Table
Represents a table and its structure.
Key methods:
-
Identification:
getSchemaName(bool $quoted = false): stringgetName(bool $quoted = false): stringgetFullName(bool $quoted = false): string
-
Metadata:
getType(): stringgetCharset(): ?stringgetCollation(): ?string
-
Columns:
getColumns(): GeneratorgetColumnsName(bool $quoted = false, ?string $tableAlias = null): arrayhasColumn(string $name): boolgetColumn(string $name): ColumngetAutoIncrementColumn(): ?Column
-
Indexes:
getIndexes(?string $type = null): GeneratorhasIndex(string $name): boolgetIndex(string $name): IndexgetPrimaryIndex(): ?Index
-
Foreign Keys:
getForeignKeys(Table $table = null): Generator
-
Other:
getSchema(): Schema
Deprecated: The
$quotedparameter ongetSchemaName(),getName(),getFullName(), andgetColumnsName()is deprecated. UseHector\Query\Statement\Quotedfor driver-aware identifier quoting.
Hector\Schema\Column
Represents a column in a table.
Key methods:
-
Identification:
getName(bool $quoted = false, ?string $tableAlias = null): stringgetFullName(bool $quoted = false): string
-
Metadata:
getPosition(): intgetDefault(): mixedisNullable(): boolgetType(): stringisAutoIncrement(): boolgetMaxlength(): ?intgetNumericPrecision(): ?intgetNumericScale(): ?intisUnsigned(): boolgetCharset(): ?stringgetCollation(): ?string
-
Other:
getTable(): TableisPrimary(): bool
Deprecated: The
$quotedparameter ongetName()andgetFullName()is deprecated. UseHector\Query\Statement\Quotedfor driver-aware identifier quoting.
Hector\Schema\Index
Represents a table index.
Key methods:
getName(): stringgetType(): stringgetColumnsName(bool $quoted = false, ?string $tableAlias = null): arraygetTable(): TablegetColumns(): arrayhasColumn(Column $column): bool
Hector\Schema\ForeignKey
Represents a foreign key constraint.
Key methods:
getName(): stringgetColumnsName(bool $quoted = false, ?string $tableAlias = null): arraygetReferencedSchemaName(): stringgetReferencedTableName(): stringgetReferencedColumnsName(bool $quoted = false, ?string $tableAlias = null): arraygetUpdateRule(): stringgetDeleteRule(): stringgetTable(): TablegetColumns(): GeneratorgetReferencedTable(): ?TablegetReferencedColumns(): Generator
Deprecated: The
$quotedparameter ongetColumnsName()andgetReferencedColumnsName()is deprecated. UseHector\Query\Statement\Quotedfor driver-aware identifier quoting.
Example: basic schema introspection
$generator = new MySQL($connection);
$schema = $generator->generateSchema('my_database');
foreach ($schema as $table) {
echo "Table: " . $table->getName() . "\n";
// Columns
foreach ($table->getColumns() as $column) {
echo " Column: " . $column->getName() . " (" . $column->getType() . ")";
echo $column->isNullable() ? " NULL" : " NOT NULL";
echo $column->isPrimary() ? " [PK]" : "";
echo "\n";
}
// Indexes
foreach ($table->getIndexes() as $index) {
echo " Index: " . $index->getName() . " (" . $index->getType() . ")";
echo " on [" . implode(', ', $index->getColumnsName()) . "]\n";
}
// Foreign keys
foreach ($table->getForeignKeys() as $fk) {
echo " FK: " . $fk->getName();
echo " [" . implode(', ', $fk->getColumnsName()) . "]";
echo " -> " . $fk->getReferencedTableName();
echo " [" . implode(', ', $fk->getReferencedColumnsName()) . "]\n";
}
echo "\n";
}
This example will output the structure of your database with all tables, columns, indexes and foreign keys. Great for CLI tools, documentation generators or migration scripts! ๐
Installation
composer require hectororm/schema