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

v6.0 2025-11-07 21:25 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

$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.

  • $connectionClass is a string representing the Connection class name.
  • $forgeClass is a string representing the Forge class name.
$forgeRegistry->map($connectionClass, $forgeClass);

Use

Load the shared Forge for a Connection.

$forge = $forgeRegistry->use($connection);

Forge dependencies will be resolved automatically from the Container.

Forges

Add Column

Add a column to a table.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • autoIncrement is 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.

  • $tableName is a string representing the table name.
  • $foreignKeyName is a string representing the foreign key name.
  • $options is an array containing the foreign key options.
    • columns is a string or array containing the columns to use for the foreign key, and will default to the foreign key name.
    • referencedTable is a string representing the referenced table to use.
    • referencedColumns is a string or array containing the columns to use in the referenced table.
    • onUpdate is a string containing the ON UPDATE operation, and will default to null.
    • onDelete is 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.

  • $tableName is a string representing the table name.
  • $indexName is a string representing the index name.
  • $options is an array containing the index options.
    • columns is a string or array containing the columns to use for the index, and will default to the index name.
    • unique is a boolean indicating whether the index must be unique, and will default to false.
    • primary is 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.

  • $tableName is a string representing the table name.
  • $options is 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.

  • $tableName is a string representing the table name.
  • $options is 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.

  • $tableName is a string representing the table name.
  • $columns is an array containing the column definitions.
  • $indexes is an array containing the index definitions.
  • $foreignKeys is an array containing the foreign key definitions.
  • $options is 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.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
$forge->dropColumn($tableName, $columnName);

Drop Foreign Key

Drop a foreign key from a table.

  • $tableName is a string representing the table name.
  • $foreignKeyName is 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.

  • $tableName is a string representing the table name.
  • $indexName is 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.

  • $tableName is a string representing the table name.
$forge->dropTable($tableName, $options);

Get Connection

Get the Connection.

$connection = $forge->getConnection();

Rename Column

Rename a column.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $newColumnName is a string representing the new column name.
$forge->renameColumn($tableName, $columnName, $newColumnName);

Rename Table

Rename a table.

  • $tableName is a string representing the table name.
  • $newTableName is 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.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • values is an array containing the enum/set values, and will default to null.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • unsigned is a boolean indicating whether the column is unsigned, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • charset is a string representing the column character set, and will default to the connection character set.
    • collation is a string representing the column collation, and will default to the connection collation.
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column, and will default to false.
    • comment is a string representing the column comment, and will default to "".
    • after is a string representing the column to add this column after, and will default to null.
    • first is 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.

  • $tableName is a string representing the table name.
  • $indexName is a string representing the index name.
  • $options is an array containing the index options.
    • columns is a string or array containing the columns to use for the index, and will default to the index name.
    • type is a string representing the index type, and will default to "BTREE".
    • unique is a boolean indicating whether the index must be unique, and will default to false.
    • primary is 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.

  • $tableName is a string representing the table name.
  • $options is an array containing the table options.
    • engine is a string representing the table engine, and will default to "InnoDB".
    • charset is a string representing the table character set, and will default to the connection character set.
    • collation is a string representing the table collation, and will default to the connection collation.
    • comment is a string representing the table comment, and will default to "".
$forge->alterTable($tableName, $options);

Build

Build a Table.

  • $tableName is a string representing the table name.
  • $options is an array containing the table options.
    • engine is a string representing the table engine, and will default to "InnoDB".
    • charset is a string representing the table character set, and will default to the connection character set.
    • collation is a string representing the table collation, and will default to the connection collation.
    • comment is 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.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $options is an array containing the column options.
    • name is a string representing the new column name.
    • type is a string representing the column type.
    • length is a number representing the column length.
    • precision is a number representing the column precision.
    • values is an array containing the enum/set values.
    • nullable is a boolean indicating whether the column is nullable.
    • unsigned is a boolean indicating whether the column is unsigned.
    • default is a string representing the column default value.
    • charset is a string representing the column character set.
    • collation is a string representing the column collation.
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column.
    • comment is a string representing the column comment.
    • after is a string representing the column to add this column after.
    • first is 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.

  • $schema is a string representing the schema name.
  • $options is an array containing the schema options.
    • charset is a string representing the schema character set, and will default to the connection character set.
    • collation is a string representing the schema collation, and will default to the connection collation.
    • ifNotExists is a boolean indicating whether to use an IF NOT EXISTS clause, and will default to false.
$forge->createSchema($schema, $options);

Create Table

Create a new table.

  • $table is a string representing the table name.
  • $columns is an array containing the column definitions.
  • $indexes is an array containing the index definitions.
  • $foreignKeys is an array containing the foreign key definitions.
  • $options is an array containing the schema options.
    • engine is a string representing the table engine, and will default to "InnoDB".
    • charset is a string representing the table character set, and will default to the connection character set.
    • collation is a string representing the table collation, and will default to the connection collation.
    • comment is 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.

  • $tableName is a string representing the table name.
$forge->dropPrimaryKey($tableName);

Drop Schema

Drop a schema.

  • $schema is a string representing the schema name.
  • $options is an array containing the schema options.
    • ifExists is a boolean indicating whether to use an IF EXISTS clause, 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.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column, and will default to false.
    • comment is 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.

  • $tableName is a string representing the table name.
  • $options is an array containing the table options.
    • comment is a string representing the table comment, and will default to "".
$forge->alterTable($tableName, $options);

Build

Build a Table.

  • $tableName is a string representing the table name.
  • $options is an array containing the table options.
    • comment is 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.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $options is an array containing the column options.
    • name is a string representing the new column name.
    • type is a string representing the column type.
    • length is a number representing the column length.
    • precision is a number representing the column precision.
    • nullable is a boolean indicating whether the column is nullable.
    • default is a string representing the column default value.
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column.
    • comment is 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.

  • $schema is a string representing the schema name.
  • $options is an array containing the schema options.
    • ifNotExists is a boolean indicating whether to use an IF NOT EXISTS clause, and will default to false.
$forge->createSchema($schema, $options);

Create Table

Create a new table.

  • $table is a string representing the table name.
  • $columns is an array containing the column definitions.
  • $indexes is an array containing the index definitions.
  • $foreignKeys is an array containing the foreign key definitions.
  • $options is an array containing the schema options.
    • comment is 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.

  • $tableName is a string representing the table name.
$forge->dropPrimaryKey($tableName);

Drop Schema

Drop a schema.

  • $schema is a string representing the schema name.
  • $options is an array containing the schema options.
    • ifExists is a boolean indicating whether to use an IF EXISTS clause, 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.

  • $tableName is a string representing the table name.
  • $columnName is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • unsigned is a boolean indicating whether the column is unsigned, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • autoIncrement is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • name is a string representing the new column name, and will default to the column name.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • autoIncrement is 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.

  • $foreignKey is a string representing the foreign key name.
  • $options is an array containing the foreign key options.
    • columns is a string or array containing the columns to use for the foreign key, and will default to the foreign key name.
    • referencedTable is a string representing the referenced table to use.
    • referencedColumns is a string or array containing the columns to use in the referenced table.
    • onUpdate is a string containing the ON UPDATE operation, and will default to null.
    • onDelete is 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.

  • $index is a string representing the index name.
  • $options is an array containing the index options.
    • columns is a string or array containing the columns to use for the index, and will default to the index name.
    • unique is a boolean indicating whether the index must be unique, and will default to false.
    • primary is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • name is 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.

  • $name is 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.

  • $column is a string representing the column name.
$table->dropColumn($column);

Drop Foreign Key

Drop a foreign key from the table.

  • $foreignKey is 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.

  • $index is 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.

  • $name is 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.

  • $name is a string representing the column name.
$hasColumn = $table->hasColumn($name);

Has Foreign Key

Determine whether the table has a foreign key.

  • $name is a string representing the foreign key name.
$hasForeignKey = $table->hasForeignKey($name);

Has Index

Determine whether the table has an index.

  • $name is a string representing the index name.
$hasIndex = $table->hasIndex($name);

Index

Get a table Index.

  • $name is a string representing the index name.
$index = $table->index($name);

Indexes

Get all table indexes.

$indexes = $table->indexes();

Rename

Rename the table.

  • $table is a string representing the new table name.
$table->rename($table);

Set Primary Key

Set the primary key.

  • $columns is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • values is an array containing the enum/set values, and will default to null.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • unsigned is a boolean indicating whether the column is unsigned, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • charset is a string representing the column character set, and will default to the connection character set.
    • collation is a string representing the column collation, and will default to the connection collation.
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column, and will default to false.
    • comment is a string representing the column comment, and will default to "".
    • after is a string representing the column to add this column after, and will default to null.
    • first is 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.

  • $index is a string representing the index name.
  • $options is an array containing the index options.
    • columns is a string or array containing the columns to use for the index, and will default to the index name.
    • type is a string representing the index type, and will default to "BTREE".
    • unique is a boolean indicating whether the index must be unique, and will default to false.
    • primary is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • name is a string representing the new column name.
    • type is a string representing the column type.
    • length is a number representing the column length.
    • precision is a number representing the column precision.
    • values is an array containing the enum/set values.
    • nullable is a boolean indicating whether the column is nullable.
    • unsigned is a boolean indicating whether the column is unsigned.
    • default is a string representing the column default value.
    • charset is a string representing the column character set.
    • collation is a string representing the column collation.
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column.
    • comment is a string representing the column comment.
    • after is a string representing the column to add this column after.
    • first is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column, and will default to false.
    • comment is 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.

  • $index is a string representing the index name.
  • $options is an array containing the index options.
    • columns is a string or array containing the columns to use for the index, and will default to the index name.
    • type is a string representing the index type, and will default to "BTREE".
    • unique is a boolean indicating whether the index must be unique, and will default to false.
    • primary is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • name is a string representing the new column name.
    • type is a string representing the column type.
    • length is a number representing the column length.
    • precision is a number representing the column precision.
    • nullable is a boolean indicating whether the column is nullable.
    • default is a string representing the column default value.
    • autoIncrement is a boolean indicating whether the column is an an auto incrementing column.
    • comment is 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.

  • $column is a string representing the column name.
  • $options is an array containing the column options.
    • type is a string representing the column type, and will default to StringType::class.
    • length is a number representing the column length, and will default to the type default.
    • precision is a number representing the column precision, and will default to the type default.
    • nullable is a boolean indicating whether the column is nullable, and will default to false.
    • unsigned is a boolean indicating whether the column is unsigned, and will default to false.
    • default is a string representing the column default value, and will default to null (no default).
    • autoIncrement is 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();