fyre / forge
A database forge library.
Installs: 165
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/fyre/forge
Requires
- fyre/container: ^2.0
- fyre/macro: ^2.0
- fyre/schema: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^12
This package is auto-updated.
Last update: 2025-11-07 21:28:48 UTC
README
FyreForge is a free, open-source database forge library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/forge
In PHP:
use Fyre\Forge\ForgeRegistry;
Basic Usage
$containeris a Container.
$forgeRegistry = new ForgeRegistry($container);
Autoloading
It is recommended to bind the ForgeRegistry to the Container as a singleton.
$container->singleton(ForgeRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$forgeRegistry = $container->use(ForgeRegistry::class);
Methods
Map
Map a Connection class to a Forge handler.
$connectionClassis a string representing the Connection class name.$forgeClassis a string representing the Forge class name.
$forgeRegistry->map($connectionClass, $forgeClass);
Use
Load the shared Forge for a Connection.
$connectionis a Connection.
$forge = $forgeRegistry->use($connection);
Forge dependencies will be resolved automatically from the Container.
Forges
Add Column
Add a column to a table.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.nullableis a boolean indicating whether the column is nullable, and will default to false.defaultis a string representing the column default value, and will default to null (no default).autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.
$forge->addColumn($tableName, $columnName, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Additional column options may be available depending on the connection handler.
Add Foreign Key
Add a foreign key to a table.
$tableNameis a string representing the table name.$foreignKeyNameis a string representing the foreign key name.$optionsis an array containing the foreign key options.columnsis a string or array containing the columns to use for the foreign key, and will default to the foreign key name.referencedTableis a string representing the referenced table to use.referencedColumnsis a string or array containing the columns to use in the referenced table.onUpdateis a string containing the ON UPDATE operation, and will default to null.onDeleteis a string containing the ON DELETE operation, and will default to null.
$forge->addForeignKey($tableName, $foreignKeyName, $options);
Foreign keys cannot be added to an existing Sqlite table.
Add Index
Add an index to a table.
$tableNameis a string representing the table name.$indexNameis a string representing the index name.$optionsis an array containing the index options.columnsis a string or array containing the columns to use for the index, and will default to the index name.uniqueis a boolean indicating whether the index must be unique, and will default to false.primaryis a boolean indicating whether the index is a primary key, and will default to false.
$forge->addIndex($tableName, $indexName, $options);
Additional index options may be available depending on the connection handler.
Primary keys cannot be added to an existing Sqlite table.
Alter Table
Alter a table.
$tableNameis a string representing the table name.$optionsis an array containing the table options.
$forge->alterTable($tableName, $options);
Additional table options may be available depending on the connection handler.
Build
Build a Table.
$tableNameis a string representing the table name.$optionsis an array containing the table options.
$table = $forge->build($tableName, $options);
Table dependencies will be resolved automatically from the Container.
Additional table options may be available depending on the connection handler.
Create Table
Create a new table.
$tableNameis a string representing the table name.$columnsis an array containing the column definitions.$indexesis an array containing the index definitions.$foreignKeysis an array containing the foreign key definitions.$optionsis an array containing the schema options.
$forge->createTable($tableName, $columns, $indexes, $foreignKeys, $options);
Additional table options may be available depending on the connection handler.
Drop Column
Drop a column from a table.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.
$forge->dropColumn($tableName, $columnName);
Drop Foreign Key
Drop a foreign key from a table.
$tableNameis a string representing the table name.$foreignKeyNameis a string representing the foreign key name.
$forge->dropForeignKey($tableName, $foreignKeyName);
Foreign keys cannot be dropped from an existing Sqlite table.
Drop Index
Drop an index from a table.
$tableNameis a string representing the table name.$indexNameis a string representing the index name.
$forge->dropIndex($tableName, $indexName);
Primary keys cannot be dropped from an existing Sqlite table.
Drop Table
Drop a table.
$tableNameis a string representing the table name.
$forge->dropTable($tableName, $options);
Get Connection
Get the Connection.
$connection = $forge->getConnection();
Rename Column
Rename a column.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$newColumnNameis a string representing the new column name.
$forge->renameColumn($tableName, $columnName, $newColumnName);
Rename Table
Rename a table.
$tableNameis a string representing the table name.$newTableNameis a string representing the new table name.
$forge->renameTable($tableName, $newTableName);
MySQL Forges
The MySQL Forge extends the Forge class and provides additional methods and options specific to MySQL databases.
Add Column
Add a column to a table.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.valuesis an array containing the enum/set values, and will default to null.nullableis a boolean indicating whether the column is nullable, and will default to false.unsignedis a boolean indicating whether the column is unsigned, and will default to false.defaultis a string representing the column default value, and will default to null (no default).charsetis a string representing the column character set, and will default to the connection character set.collationis a string representing the column collation, and will default to the connection collation.autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.commentis a string representing the column comment, and will default to "".afteris a string representing the column to add this column after, and will default to null.firstis a boolean indicating whether to add this column first in the table, and will default to false.
$forge->addColumn($tableName, $columnName, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Add Index
Add an index to a table.
$tableNameis a string representing the table name.$indexNameis a string representing the index name.$optionsis an array containing the index options.columnsis a string or array containing the columns to use for the index, and will default to the index name.typeis a string representing the index type, and will default to "BTREE".uniqueis a boolean indicating whether the index must be unique, and will default to false.primaryis a boolean indicating whether the index is a primary key, and will default to false.
$forge->addIndex($tableName, $indexName, $options);
Alter Table
Alter a table.
$tableNameis a string representing the table name.$optionsis an array containing the table options.engineis a string representing the table engine, and will default to "InnoDB".charsetis a string representing the table character set, and will default to the connection character set.collationis a string representing the table collation, and will default to the connection collation.commentis a string representing the table comment, and will default to "".
$forge->alterTable($tableName, $options);
Build
Build a Table.
$tableNameis a string representing the table name.$optionsis an array containing the table options.engineis a string representing the table engine, and will default to "InnoDB".charsetis a string representing the table character set, and will default to the connection character set.collationis a string representing the table collation, and will default to the connection collation.commentis a string representing the table comment, and will default to "".
$table = $forge->build($tableName, $options);
Table dependencies will be resolved automatically from the Container.
Change Column
Change a table column.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$optionsis an array containing the column options.nameis a string representing the new column name.typeis a string representing the column type.lengthis a number representing the column length.precisionis a number representing the column precision.valuesis an array containing the enum/set values.nullableis a boolean indicating whether the column is nullable.unsignedis a boolean indicating whether the column is unsigned.defaultis a string representing the column default value.charsetis a string representing the column character set.collationis a string representing the column collation.autoIncrementis a boolean indicating whether the column is an an auto incrementing column.commentis a string representing the column comment.afteris a string representing the column to add this column after.firstis a boolean indicating whether to add this column first in the table.
$forge->changeColumn($tableName, $columnName, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Unspecified options will default to the current value.
Create Schema
Create a new schema.
$schemais a string representing the schema name.$optionsis an array containing the schema options.charsetis a string representing the schema character set, and will default to the connection character set.collationis a string representing the schema collation, and will default to the connection collation.ifNotExistsis a boolean indicating whether to use anIF NOT EXISTSclause, and will default to false.
$forge->createSchema($schema, $options);
Create Table
Create a new table.
$tableis a string representing the table name.$columnsis an array containing the column definitions.$indexesis an array containing the index definitions.$foreignKeysis an array containing the foreign key definitions.$optionsis an array containing the schema options.engineis a string representing the table engine, and will default to "InnoDB".charsetis a string representing the table character set, and will default to the connection character set.collationis a string representing the table collation, and will default to the connection collation.commentis a string representing the table comment, and will default to "".
$forge->createTable($table, $columns, $indexes, $foreignKeys, $options);
Drop Primary Key
Drop a primary key from a table.
$tableNameis a string representing the table name.
$forge->dropPrimaryKey($tableName);
Drop Schema
Drop a schema.
$schemais a string representing the schema name.$optionsis an array containing the schema options.ifExistsis a boolean indicating whether to use anIF EXISTSclause, and will default to false.
$forge->dropSchema($schema, $options);
Postgres
The Postgres Forge extends the Forge class and provides additional methods and options specific to Postgres databases.
Add Column
Add a column to a table.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.nullableis a boolean indicating whether the column is nullable, and will default to false.defaultis a string representing the column default value, and will default to null (no default).autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.commentis a string representing the column comment, and will default to "".
$forge->addColumn($tableName, $columnName, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Alter Table
Alter a table.
$tableNameis a string representing the table name.$optionsis an array containing the table options.commentis a string representing the table comment, and will default to "".
$forge->alterTable($tableName, $options);
Build
Build a Table.
$tableNameis a string representing the table name.$optionsis an array containing the table options.commentis a string representing the table comment, and will default to "".
$table = $forge->build($tableName, $options);
Table dependencies will be resolved automatically from the Container.
Change Column
Change a table column.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$optionsis an array containing the column options.nameis a string representing the new column name.typeis a string representing the column type.lengthis a number representing the column length.precisionis a number representing the column precision.nullableis a boolean indicating whether the column is nullable.defaultis a string representing the column default value.autoIncrementis a boolean indicating whether the column is an an auto incrementing column.commentis a string representing the column comment.
$forge->changeColumn($tableName, $columnName, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Unspecified options will default to the current value.
Create Schema
Create a new schema.
$schemais a string representing the schema name.$optionsis an array containing the schema options.ifNotExistsis a boolean indicating whether to use anIF NOT EXISTSclause, and will default to false.
$forge->createSchema($schema, $options);
Create Table
Create a new table.
$tableis a string representing the table name.$columnsis an array containing the column definitions.$indexesis an array containing the index definitions.$foreignKeysis an array containing the foreign key definitions.$optionsis an array containing the schema options.commentis a string representing the table comment, and will default to "".
$forge->createTable($tableName, $columns, $indexes, $foreignKeys, $options);
Drop Primary Key
Drop a primary key from a table.
$tableNameis a string representing the table name.
$forge->dropPrimaryKey($tableName);
Drop Schema
Drop a schema.
$schemais a string representing the schema name.$optionsis an array containing the schema options.ifExistsis a boolean indicating whether to use anIF EXISTSclause, and will default to false.
$forge->dropSchema($schema, $options);
Sqlite
The Sqlite Forge extends the Forge class.
Add Column
Add a column to a table.
$tableNameis a string representing the table name.$columnNameis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.nullableis a boolean indicating whether the column is nullable, and will default to false.unsignedis a boolean indicating whether the column is unsigned, and will default to false.defaultis a string representing the column default value, and will default to null (no default).autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.
$forge->addColumn($tableName, $columnName, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Tables
Add Column
Add a column to the table.
$columnis a string representing the column name.$optionsis an array containing the column options.nameis a string representing the new column name, and will default to the column name.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.nullableis a boolean indicating whether the column is nullable, and will default to false.defaultis a string representing the column default value, and will default to null (no default).autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.
$table->addColumn($column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Additional column options may be available depending on the connection handler.
Add Foreign Key
Add a foreign key to the table.
$foreignKeyis a string representing the foreign key name.$optionsis an array containing the foreign key options.columnsis a string or array containing the columns to use for the foreign key, and will default to the foreign key name.referencedTableis a string representing the referenced table to use.referencedColumnsis a string or array containing the columns to use in the referenced table.onUpdateis a string containing the ON UPDATE operation, and will default to null.onDeleteis a string containing the ON DELETE operation, and will default to null.
$table->addForeignKey($foreignKey, $options);
Foreign keys cannot be added to an existing Sqlite table.
Add Index
Add an index to the table.
$indexis a string representing the index name.$optionsis an array containing the index options.columnsis a string or array containing the columns to use for the index, and will default to the index name.uniqueis a boolean indicating whether the index must be unique, and will default to false.primaryis a boolean indicating whether the index is a primary key, and will default to false.
$table->addIndex($index, $options);
Additional index options may be available depending on the connection handler.
Primary keys cannot be added to an existing Sqlite table.
Change Column
Change a table column.
$columnis a string representing the column name.$optionsis an array containing the column options.nameis a string representing the new column name, and will default to the column name.
$table->changeColumn($column, $options);
Additional column options may be available depending on the connection handler.
Column definitions can not be modified for an existing Sqlite table.
Clear
Clear the column and index data.
$table->clear();
Column
Get a Column.
$nameis a string representing the column name.
$column = $table->column($name);
Column Names
Get the names of all table columns.
$columnNames = $table->columnNames();
Columns
Get all table columns.
$columns = $table->columns();
Drop
Drop the table.
$table->drop();
Drop Column
Drop a column from the table.
$columnis a string representing the column name.
$table->dropColumn($column);
Drop Foreign Key
Drop a foreign key from the table.
$foreignKeyis a string representing the foreign key name.
$table->dropForeignKey($foreignKey);
Foreign keys cannot be dropped from an existing Sqlite table.
Drop Index
Drop an index from the table.
$indexis a string representing the index name.
$table->dropIndex($index);
Primary keys cannot be dropped from an existing Sqlite table.
Execute
Generate and execute the SQL queries.
$table->execute();
Foreign Key
Get a table ForeignKey.
$nameis a string representing the foreign key name.
$foreignKey = $table->foreignKey($name);
Foreign Keys
Get all table foreign keys.
$foreignKeys = $table->foreignKeys();
Get Comment
Get the table comment.
$comment = $table->getComment();
Get Forge
Get the Forge.
$forge = $table->getForge();
Get Name
Get the table name.
$name = $table->getName();
Has Column
Determine whether the table has a column.
$nameis a string representing the column name.
$hasColumn = $table->hasColumn($name);
Has Foreign Key
Determine whether the table has a foreign key.
$nameis a string representing the foreign key name.
$hasForeignKey = $table->hasForeignKey($name);
Has Index
Determine whether the table has an index.
$nameis a string representing the index name.
$hasIndex = $table->hasIndex($name);
Index
Get a table Index.
$nameis a string representing the index name.
$index = $table->index($name);
Indexes
Get all table indexes.
$indexes = $table->indexes();
Rename
Rename the table.
$tableis a string representing the new table name.
$table->rename($table);
Set Primary Key
Set the primary key.
$columnsis a string or array containing the columns to use for the primary key.
$table->setPrimaryKey($columns);
Primary keys cannot be added to an existing Sqlite table.
SQL
Generate the SQL queries.
$queries = $table->sql();
To Array
Get the table data as an array.
$data = $table->toArray();
MySQL Tables
Add Column
Add a column to the table.
$columnis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.valuesis an array containing the enum/set values, and will default to null.nullableis a boolean indicating whether the column is nullable, and will default to false.unsignedis a boolean indicating whether the column is unsigned, and will default to false.defaultis a string representing the column default value, and will default to null (no default).charsetis a string representing the column character set, and will default to the connection character set.collationis a string representing the column collation, and will default to the connection collation.autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.commentis a string representing the column comment, and will default to "".afteris a string representing the column to add this column after, and will default to null.firstis a boolean indicating whether to add this column first in the table, and will default to false.
$table->addColumn($column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Add Index
Add an index to the table.
$indexis a string representing the index name.$optionsis an array containing the index options.columnsis a string or array containing the columns to use for the index, and will default to the index name.typeis a string representing the index type, and will default to "BTREE".uniqueis a boolean indicating whether the index must be unique, and will default to false.primaryis a boolean indicating whether the index is a primary key, and will default to false.
$table->addIndex($index, $options);
Change Column
Change a table column.
$columnis a string representing the column name.$optionsis an array containing the column options.nameis a string representing the new column name.typeis a string representing the column type.lengthis a number representing the column length.precisionis a number representing the column precision.valuesis an array containing the enum/set values.nullableis a boolean indicating whether the column is nullable.unsignedis a boolean indicating whether the column is unsigned.defaultis a string representing the column default value.charsetis a string representing the column character set.collationis a string representing the column collation.autoIncrementis a boolean indicating whether the column is an an auto incrementing column.commentis a string representing the column comment.afteris a string representing the column to add this column after.firstis a boolean indicating whether to add this column first in the table.
$table->changeColumn($column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Unspecified options will default to the current value.
Get Charset
Get the table character set.
$charset = $table->getCharset();
Get Collation
Get the table collation.
$collation = $table->getCollation();
Get Engine
Get the table engine.
$engine = $table->getEngine();
Postgres Tables
Add Column
Add a column to the table.
$columnis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.nullableis a boolean indicating whether the column is nullable, and will default to false.defaultis a string representing the column default value, and will default to null (no default).autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.commentis a string representing the column comment, and will default to "".
$table->addColumn($column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Add Index
Add an index to the table.
$indexis a string representing the index name.$optionsis an array containing the index options.columnsis a string or array containing the columns to use for the index, and will default to the index name.typeis a string representing the index type, and will default to "BTREE".uniqueis a boolean indicating whether the index must be unique, and will default to false.primaryis a boolean indicating whether the index is a primary key, and will default to false.
$table->addIndex($index, $options);
Change Column
Change a table column.
$columnis a string representing the column name.$optionsis an array containing the column options.nameis a string representing the new column name.typeis a string representing the column type.lengthis a number representing the column length.precisionis a number representing the column precision.nullableis a boolean indicating whether the column is nullable.defaultis a string representing the column default value.autoIncrementis a boolean indicating whether the column is an an auto incrementing column.commentis a string representing the column comment.
$table->changeColumn($column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Unspecified options will default to the current value.
Sqlite Tables
Add Column
Add a column to the table.
$columnis a string representing the column name.$optionsis an array containing the column options.typeis a string representing the column type, and will default toStringType::class.lengthis a number representing the column length, and will default to the type default.precisionis a number representing the column precision, and will default to the type default.nullableis a boolean indicating whether the column is nullable, and will default to false.unsignedis a boolean indicating whether the column is unsigned, and will default to false.defaultis a string representing the column default value, and will default to null (no default).autoIncrementis a boolean indicating whether the column is an an auto incrementing column, and will default to false.
$table->addColumn($column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Columns
Get Comment
Get the column comment.
$comment = $column->getComment();
Get Default
Get the column default value.
$default = $column->getDefault();
Get Length
Get the column length.
$length = $column->getLength();
Get Name
Get the column name.
$name = $column->getName();
Get Precision
Get the column precision.
$precision = $column->getPrecision();
Get Table
Get the Table.
$table = $column->getTable();
Get Type
Get the column type.
$type = $column->getType();
Is Auto Increment
Determine whether the column is an auto increment column.
$isAutoIncrement = $column->isAutoIncrement();
Is Nullable
Determine whether the column is nullable.
$isNullable = $column->isNullable();
Is Unsigned
Determine whether the column is unsigned.
$isUnsigned = $column->isUnsigned();
To Array
Get the column data as an array.
$data = $column->toArray();
MySQL Columns
Get Charset
Get the column character set.
$charset = $column->getCharset();
Get Collation
Get the column collation.
$collation = $column->getCollation();
Get Values
Get the column enum values.
$values = $column->getValues();
Indexes
Get Columns
Get the column names.
$columns = $index->getColumns();
Get Name
Get the index name.
$name = $index->getName();
Get Table
Get the Table.
$table = $index->getTable();
Get Type
Get the index type.
$type = $index->getType();
Is Primary
Determine whether the index is primary.
$isPrimary = $index->isPrimary();
Is Unique
Determine whether the index is unique.
$isUnique = $index->isUnique();
To Array
Get the index data as an array.
$data = $index->toArray();
Foreign Keys
Get Columns
Get the column names.
$columns = $foreignKey->getColumns();
Get Name
$name = $foreignKey->getName();
Get On Delete
Get the delete action.
$onDelete = $foreignKey->getOnDelete();
Get On Update
Get the update action.
$onUpdate = $foreignKey->getOnUpdate();
Get Referenced Columns
Get the referenced column names.
$referencedColumn = $foreignKey->getReferencedColumns();
Get Referenced Table
Get the referenced table name.
$referencedTable = $foreignKey->getReferencedTable();
Get Table
Get the Table.
$table = $foreignKey->getTable();
To Array
Get the foreign key data as an array.
$data = $foreignKey->toArray();