fyre / schema
A database schema library.
Requires
- fyre/cache: ^5.0
- fyre/container: ^1.0
- fyre/db: ^6.0
- fyre/typeparser: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
This package is auto-updated.
Last update: 2024-11-08 12:33:55 UTC
README
FyreSchema is a free, open-source database schema library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/schema
In PHP:
use Fyre\Schema\SchemaRegistry;
Basic Usage
$container
is a Container.
$schemaRegistry = new SchemaRegistry($container);
Autoloading
It is recommended to bind the SchemaRegistry to the Container as a singleton.
$container->singleton(SchemaRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$schemaRegistry = $container->use(SchemaRegistry::class);
Methods
Map
Map a Connection class to a Schema handler.
$connectionClass
is a string representing the Connection class name.$schemaClass
is a string representing the Schema class name.
$schemaRegistry->map($connectionClass, $schemaClass);
Use
Load the shared Schema for a Connection.
$connection
is a Connection.
$schema = $schemaRegistry->use($connection);
Schema dependencies will be resolved automatically from the Container.
Schemas
Clear
Clear data from the cache.
$schema->clear();
Describe
Get the TableSchema for a table.
$name
is a string representing the table name.
$tableSchema = $schema->describe($name);
TableSchema dependencies will be resolved automatically from the Container.
Get Connection
Get the Connection.
$connection = $schema->getConnection();
Get Database Name
Get the database name.
$database = $schema->getDatabaseName();
Has Table
Determine whether the schema has a table.
$name
is a string representing the table name.
$hasTable = $schema->hasTable($name);
Table
Get the data for a table.
$name
is a string representing the table name.
$table = $schema->table($name);
Table Names
Get the names of all schema tables.
$tableNames = $schema->tableNames();
Tables
Get the data for all schema tables.
$tables = $schema->tables();
Table Schemas
Clear
Clear data from the cache.
$tableSchema->clear();
Column
Get the data for a table column.
$name
is a string representing the column name.
$column = $tableSchema->column($name);
Column Names
Get the names of all table columns.
$columnNames = $tableSchema->columnNames();
Columns
Get the data for all table columns.
$columns = $tableSchema->columns();
Default Value
Get the default value for a column.
$name
is a string representing the column name.
$defaultValue = $tableSchema->defaultValue($name);
This method will evaluate expression values (eg. current_timestamp()).
Foreign Key
Get the data for a table foreign key.
$name
is a string representing the foreign key name.
$foreignKey = $tableSchema->foreignKey($name);
Foreign Keys
Get the data for all table foreign keys.
$foreignKeys = $tableSchema->foreignKeys();
Get Schema
Get the Schema.
$schema = $tableSchema->getSchema();
Get Table Name
Get the table name.
$tableName = $tableSchema->getTableName();
Get Type
Get the Type parser for a column.
$name
is a string representing the column name.
$parser = $tableSchema->getType($name);
Has Auto Increment
Determine whether the table has an auto increment column.
$hasAutoIncrement = $tableSchema->hasAutoIncrement();
Has Column
Determine whether the table has a column.
$name
is a string representing the column name.
$hasColumn = $tableSchema->hasColumn($name);
Has Foreign Key
Determine whether the table has a foreign key.
$name
is a string representing the foreign key name.
$hasForeignKey = $tableSchema->hasForeignKey($name);
Has Index
Determine whether the table has an index.
$name
is a string representing the index name.
$hasIndex = $tableSchema->hasIndex($name);
Index
Get the data for a table index.
$name
is a string representing the index name.
$index = $tableSchema->index($name);
Indexes
Get the data for all table indexes.
$indexes = $tableSchema->indexes();
Is Nullable
Determine whether a table column is nullable.
$name
is a string representing the column name.
$isNullable = $tableSchema->isNullable($name);
Primary Key
Get the primary key for the table.
$primaryKey = $tableSchema->primaryKey();