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: ^1.0
- fyre/schema: ^6.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-03 11:05:55 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.
$tableis a string representing the table name.$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.
$forge->addColumn($table, $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 Index
Add an index to a table.
$tableis a string representing the table name.$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.
$forge->addIndex($table, $index, $options);
Additional index options may be available depending on the connection handler.
Build
Build a TableForge.
$tableis a string representing the table name.
$tableForge = $forge->build($table);
TableForge 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.
$tableis a string representing the table name.$columnsis an array containing the column definitions.$optionsis an array containing the schema options.indexesis an array containing the index definitions.foreignKeysis an array containing the foreign key definitions.ifNotExistsis a boolean indicating whether to use anIF NOT EXISTSclause, and will default to false.
$forge->createTable($table, $columns, $options);
Additional table options may be available depending on the connection handler.
Drop Column
Drop a column from a table.
$tableis a string representing the table name.$columnis a string representing the column name.$optionsis an array containing the column options.ifExistsis a boolean indicating whether to use anIF EXISTSclause, and will default to false.
$forge->dropColumn($table, $column, $options);
Drop Index
Drop an index from a table.
$tableis a string representing the table name.$indexis a string representing the index name.
$forge->dropIndex($table, $index);
Drop Table
Drop a table.
$tableis a string representing the table name.$optionsis an array containing the table options.ifExistsis a boolean indicating whether to use anIF EXISTSclause, and will default to false.
$forge->dropTable($table, $options);
Get Connection
Get the Connection.
$connection = $forge->getConnection();
Rename Column
Rename a column.
$tableis a string representing the table name.$columnis a string representing the column name.$newColumnis a string representing the new column name.
$forge->renameColumn($table, $column, $newColumn);
Rename Table
Rename a table.
$tableis a string representing the table name.$newTableis a string representing the new table name.
$forge->renameTable($table, $newTable);
MySQL
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.
$tableis a string representing the table name.$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.
$forge->addColumn($table, $column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Add Foreign Key
Add a foreign key to a table.
$tableis a string representing the table name.$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.updateis a string containing the ON UPDATE operation, and will default to "".deleteis a string containing the ON DELETE operation, and will default to "".
$forge->addForeignKey($table, $foreignKey, $options);
Add Index
Add an index to a table.
$tableis a string representing the table name.$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.
$forge->addIndex($table, $index, $options);
Alter Table
Alter a table.
$tableis 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($table, $options);
Build
Build a TableForge.
$tableis 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 "".
$tableForge = $forge->build($table, $options);
TableForge dependencies will be resolved automatically from the Container.
Change Column
Change a table column.
$tableis a string representing the table name.$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.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->changeColumn($table, $column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
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.$optionsis an array containing the schema options.indexesis an array containing the index definitions.foreignKeysis an array containing the foreign key definitions.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 "".ifNotExistsis a boolean indicating whether to use anIF NOT EXISTSclause, and will default to false.
$forge->createTable($table, $columns, $options);
Drop Foreign Key
Drop a foreign key from a table.
$tableis a string representing the table name.$foreignKeyis a string representing the foreign key name.
$forge->dropForeignKey($table, $foreignKey);
Drop Primary Key
Drop a primary key from a table.
$tableis a string representing the table name.
$forge->dropPrimaryKey();
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.
$tableis a string representing the table name.$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 "".
$forge->addColumn($table, $column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Add Foreign Key
Add a foreign key to a table.
$tableis a string representing the table name.$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.updateis a string containing the ON UPDATE operation, and will default to "".deleteis a string containing the ON DELETE operation, and will default to "".
$forge->addForeignKey($table, $foreignKey, $options);
Alter Column Auto Increment
Alter a column's auto increment.
$tableis a string representing the table name.$columnis a string representing the column name.$autoIncrementis a boolean indicating whether the column is an an auto incrementing column.
$forge->alterColumnAutoIncrement($table, $column, $autoIncrement);
Alter Column Default
Alter a column's default value.
$tableis a string representing the table name.$columnis a string representing the column name.$defaultis a string representing the column default value.
$forge->alterColumnDefault($table, $column, $default);
Alter Column Nullable
Alter whether a column is nullable.
$tableis a string representing the table name.$columnis a string representing the column name.$nullableis a boolean indicating whether the column is nullable, and will default to false.
$forge->alterColumnNullable($table, $column, $nullable);
Alter Column Type
Alter a column's type.
$tableis a string representing the table name.$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.
$forge->alterColumnType($table, $column, $options);
You can also specify a Type class name as the type, which will be automatically mapped to the correct type.
Build
Build a TableForge.
$tableis a string representing the table name.$optionsis an array containing the table options.commentis a string representing the table comment, and will default to "".
$tableForge = $forge->build($table, $options);
TableForge dependencies will be resolved automatically from the Container.
Comment On Column
Set the comment for a column.
$tableis a string representing the table name.$columnis a string representing the column name.$commentis a string representing the table comment.
$forge->commentOnTable($table, $column, $comment);
Comment On Table
Set the comment for a table.
$tableis a string representing the table name.$commentis a string representing the table comment.
$forge->commentOnTable($table, $comment);
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.$optionsis an array containing the schema options.indexesis an array containing the index definitions.foreignKeysis an array containing the foreign key definitions.commentis a string representing the table comment, and will default to "".ifNotExistsis a boolean indicating whether to use anIF NOT EXISTSclause, and will default to false.
$forge->createTable($table, $columns, $options);
Drop Constraint
Drop a constraint from a table.
$tableis a string representing the table name.$indexis a string representing the index name.
$forge->dropConstraint($table, $index);
Drop Foreign Key
Drop a foreign key from a table.
$tableis a string representing the table name.$foreignKeyis a string representing the foreign key name.
$forge->dropForeignKey($table, $foreignKey);
Drop Primary Key
Drop a primary key from a table.
$tableis a string representing the table name.
$forge->dropPrimaryKey();
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.
Table Forges
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.
$tableForge->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.updateis a string containing the ON UPDATE operation, and will default to "".deleteis a string containing the ON DELETE operation, and will default to "".
$tableForge->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.
$tableForge->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.
$tableForge->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.
$tableForge->clear();
Column
Get the data for a table column.
$nameis a string representing the column name.
$column = $tableForge->column($name);
Column Names
Get the names of all table columns.
$columnNames = $tableForge->columnNames();
Columns
Get the data for all table columns.
$columns = $tableForge->columns();
Drop
Drop the table.
$tableForge->drop();
Drop Column
Drop a column from the table.
$columnis a string representing the column name.
$tableForge->dropColumn($column);
Drop Foreign Key
Drop a foreign key from the table.
$foreignKeyis a string representing the foreign key name.
$tableForge->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.
$tableForge->dropIndex($index);
Primary and unique keys cannot be dropped from an existing Sqlite table.
Execute
Generate and execute the SQL queries.
$tableForge->execute();
Foreign Key
Get the data for a table foreign key.
$nameis a string representing the foreign key name.
$foreignKey = $tableForge->foreignKey($name);
Foreign Keys
Get the data for all table foreign keys.
$foreignKeys = $tableForge->foreignKeys();
Get Forge
Get the Forge.
$forge = $tableForge->getForge();
Get Table Name
Get the table name.
$tableName = $tableForge->getTableName();
Has Column
Determine whether the table has a column.
$nameis a string representing the column name.
$hasColumn = $tableForge->hasColumn($name);
Has Foreign Key
Determine whether the table has a foreign key.
$nameis a string representing the foreign key name.
$hasForeignKey = $tableForge->hasForeignKey($name);
Has Index
Determine whether the table has an index.
$nameis a string representing the index name.
$hasIndex = $tableForge->hasIndex($name);
Index
Get the data for a table index.
$nameis a string representing the index name.
$index = $tableForge->index($name);
Indexes
Get the data for all table indexes.
$indexes = $tableForge->indexes();
Rename
Rename the table.
$tableis a string representing the new table name.
$tableForge->rename($table);
Set Primary Key
Set the primary key.
$columnsis a string or array containing the columns to use for the primary key.
$tableForge->setPrimaryKey($columns);
Primary keys cannot be added to an existing Sqlite table.
SQL
Generate the SQL queries.
$queries = $tableForge->sql();