TableSchema
in package
implements
TableSchemaInterface, SqlGeneratorInterface
Represents a single table in a database schema.
Can either be populated using the reflection API's or by incrementally building an instance using methods.
Once created TableSchema instances can be added to Schema\Collection objects. They can also be converted into SQL using the createSql(), dropSql() and truncateSql() methods.
Table of Contents
Interfaces
- TableSchemaInterface
- An interface used by database TableSchema objects.
- SqlGeneratorInterface
- An interface used by TableSchema objects.
Constants
- ACTION_CASCADE = 'cascade'
- Foreign key cascade action
- ACTION_NO_ACTION = 'noAction'
- Foreign key no action
- ACTION_RESTRICT = 'restrict'
- Foreign key restrict action
- ACTION_SET_DEFAULT = 'setDefault'
- Foreign key restrict default
- ACTION_SET_NULL = 'setNull'
- Foreign key set null action
- CONSTRAINT_FOREIGN = 'foreign'
- Foreign constraint type
- CONSTRAINT_PRIMARY = 'primary'
- Primary constraint type
- CONSTRAINT_UNIQUE = 'unique'
- Unique constraint type
- INDEX_FULLTEXT = 'fulltext'
- Fulltext index type
- INDEX_INDEX = 'index'
- Index - index type
- LENGTH_LONG = 4294967295
- Column length when using a `long` column type
- LENGTH_MEDIUM = 16777215
- Column length when using a `medium` column type
- LENGTH_TINY = 255
- Column length when using a `tiny` column type
Properties
- $columnLengths : array<string, int>
- Valid column length that can be used with text type columns
- $_columnExtras : array<string, array<string, mixed>>
- Additional type specific properties.
- $_columnKeys : array<string, mixed>
- The valid keys that can be used in a column definition.
- $_columns : array<string, array<string|int, mixed>>
- Columns in the table.
- $_constraints : array<string, array<string, mixed>>
- Constraints in the table.
- $_indexes : array<string, array<string|int, mixed>>
- Indexes in the table.
- $_indexKeys : array<string, mixed>
- The valid keys that can be used in an index definition.
- $_options : array<string, mixed>
- Options for the table.
- $_table : string
- The name of the table
- $_temporary : bool
- Whether the table is temporary
- $_typeMap : array<string, string>
- A map with columns to types
- $_validConstraintTypes : array<string|int, string>
- Names of the valid constraint types.
- $_validForeignKeyActions : array<string|int, string>
- Names of the valid foreign key actions.
- $_validIndexTypes : array<string|int, string>
- Names of the valid index types.
Methods
- __construct() : mixed
- Constructor.
- __debugInfo() : array<string, mixed>
- Returns an array of the table schema.
- addColumn() : $this
- Add a column to the table.
- addConstraint() : $this
- Add a constraint.
- addConstraintSql() : array<string|int, mixed>
- Generate the SQL statements to add the constraints to the table
- addIndex() : $this
- Add an index.
- baseColumnType() : string|null
- Returns the base type name for the provided column.
- columns() : array<string|int, string>
- Get the column names in the table.
- constraints() : array<string|int, string>
- Get the names of all the constraints in the table.
- createSql() : array<string|int, mixed>
- Generate the SQL to create the Table.
- defaultValues() : array<string, mixed>
- Get a hash of columns and their default values.
- dropConstraint() : $this
- Remove a constraint.
- dropConstraintSql() : array<string|int, mixed>
- Generate the SQL statements to drop the constraints to the table
- dropSql() : array<string|int, mixed>
- Generate the SQL to drop a table.
- getColumn() : array<string, mixed>|null
- Get column data in the table.
- getColumnType() : string|null
- Returns column type or null if a column does not exist.
- getConstraint() : array<string, mixed>|null
- Read information about a constraint based on name.
- getIndex() : array<string, mixed>|null
- Read information about an index based on name.
- getOptions() : array<string, mixed>
- Gets the options for a table.
- getPrimaryKey() : array<string|int, string>
- Get the column(s) used for the primary key.
- hasAutoincrement() : bool
- Check whether a table has an autoIncrement column defined.
- hasColumn() : bool
- Returns true if a column exists in the schema.
- indexes() : array<string|int, string>
- Get the names of all the indexes in the table.
- isNullable() : bool
- Check whether a field is nullable
- isTemporary() : bool
- Gets whether the table is temporary in the database.
- name() : string
- Get the name of the table.
- primaryKey() : array<string|int, mixed>
- Get the column(s) used for the primary key.
- removeColumn() : $this
- Remove a column from the table schema.
- setColumnType() : $this
- Sets the type of a column.
- setOptions() : $this
- Sets the options for a table.
- setTemporary() : $this
- Sets whether the table is temporary in the database.
- truncateSql() : array<string|int, mixed>
- Generate the SQL statements to truncate a table
- typeMap() : array<string, string>
- Returns an array where the keys are the column names in the schema and the values the database type they have.
- _checkForeignKey() : array<string, mixed>
- Helper method to check/validate foreign keys.
Constants
ACTION_CASCADE
Foreign key cascade action
public
string
ACTION_CASCADE
= 'cascade'
ACTION_NO_ACTION
Foreign key no action
public
string
ACTION_NO_ACTION
= 'noAction'
ACTION_RESTRICT
Foreign key restrict action
public
string
ACTION_RESTRICT
= 'restrict'
ACTION_SET_DEFAULT
Foreign key restrict default
public
string
ACTION_SET_DEFAULT
= 'setDefault'
ACTION_SET_NULL
Foreign key set null action
public
string
ACTION_SET_NULL
= 'setNull'
CONSTRAINT_FOREIGN
Foreign constraint type
public
string
CONSTRAINT_FOREIGN
= 'foreign'
CONSTRAINT_PRIMARY
Primary constraint type
public
string
CONSTRAINT_PRIMARY
= 'primary'
CONSTRAINT_UNIQUE
Unique constraint type
public
string
CONSTRAINT_UNIQUE
= 'unique'
INDEX_FULLTEXT
Fulltext index type
public
string
INDEX_FULLTEXT
= 'fulltext'
INDEX_INDEX
Index - index type
public
string
INDEX_INDEX
= 'index'
LENGTH_LONG
Column length when using a `long` column type
public
int
LENGTH_LONG
= 4294967295
LENGTH_MEDIUM
Column length when using a `medium` column type
public
int
LENGTH_MEDIUM
= 16777215
LENGTH_TINY
Column length when using a `tiny` column type
public
int
LENGTH_TINY
= 255
Properties
$columnLengths
Valid column length that can be used with text type columns
public
static array<string, int>
$columnLengths
= ['tiny' => self::LENGTH_TINY, 'medium' => self::LENGTH_MEDIUM, 'long' => self::LENGTH_LONG]
$_columnExtras
Additional type specific properties.
protected
static array<string, array<string, mixed>>
$_columnExtras
= ['string' => ['collate' => null], 'char' => ['collate' => null], 'text' => ['collate' => null], 'tinyinteger' => ['unsigned' => null], 'smallinteger' => ['unsigned' => null], 'integer' => ['unsigned' => null, 'autoIncrement' => null], 'biginteger' => ['unsigned' => null, 'autoIncrement' => null], 'decimal' => ['unsigned' => null], 'float' => ['unsigned' => null]]
$_columnKeys
The valid keys that can be used in a column definition.
protected
static array<string, mixed>
$_columnKeys
= ['type' => null, 'baseType' => null, 'length' => null, 'precision' => null, 'null' => null, 'default' => null, 'comment' => null]
$_columns
Columns in the table.
protected
array<string, array<string|int, mixed>>
$_columns
= []
$_constraints
Constraints in the table.
protected
array<string, array<string, mixed>>
$_constraints
= []
$_indexes
Indexes in the table.
protected
array<string, array<string|int, mixed>>
$_indexes
= []
$_indexKeys
The valid keys that can be used in an index definition.
protected
static array<string, mixed>
$_indexKeys
= ['type' => null, 'columns' => [], 'length' => [], 'references' => [], 'update' => 'restrict', 'delete' => 'restrict']
$_options
Options for the table.
protected
array<string, mixed>
$_options
= []
$_table
The name of the table
protected
string
$_table
$_temporary
Whether the table is temporary
protected
bool
$_temporary
= false
$_typeMap
A map with columns to types
protected
array<string, string>
$_typeMap
= []
$_validConstraintTypes
Names of the valid constraint types.
protected
static array<string|int, string>
$_validConstraintTypes
= [self::CONSTRAINT_PRIMARY, self::CONSTRAINT_UNIQUE, self::CONSTRAINT_FOREIGN]
$_validForeignKeyActions
Names of the valid foreign key actions.
protected
static array<string|int, string>
$_validForeignKeyActions
= [self::ACTION_CASCADE, self::ACTION_SET_NULL, self::ACTION_SET_DEFAULT, self::ACTION_NO_ACTION, self::ACTION_RESTRICT]
$_validIndexTypes
Names of the valid index types.
protected
static array<string|int, string>
$_validIndexTypes
= [self::INDEX_INDEX, self::INDEX_FULLTEXT]
Methods
__construct()
Constructor.
public
__construct(string $table[, array<string, array<string|int, mixed>|string> $columns = [] ]) : mixed
Parameters
- $table : string
-
The table name.
- $columns : array<string, array<string|int, mixed>|string> = []
-
The list of columns for the schema.
__debugInfo()
Returns an array of the table schema.
public
__debugInfo() : array<string, mixed>
Return values
array<string, mixed>addColumn()
Add a column to the table.
public
addColumn(string $name, mixed $attrs) : $this
Parameters
- $name : string
-
The name of the column
- $attrs : mixed
-
The attributes for the column or the type name.
Tags
Return values
$thisaddConstraint()
Add a constraint.
public
addConstraint(string $name, mixed $attrs) : $this
Parameters
- $name : string
-
The name of the constraint.
- $attrs : mixed
-
The attributes for the constraint. If string it will be used as
type
.
Tags
Return values
$thisaddConstraintSql()
Generate the SQL statements to add the constraints to the table
public
addConstraintSql(Connection $connection) : array<string|int, mixed>
Parameters
- $connection : Connection
-
The connection to generate SQL for.
Tags
Return values
array<string|int, mixed> —SQL to add the constraints.
addIndex()
Add an index.
public
addIndex(string $name, mixed $attrs) : $this
Parameters
- $name : string
-
The name of the index.
- $attrs : mixed
-
The attributes for the index. If string it will be used as
type
.
Tags
Return values
$thisbaseColumnType()
Returns the base type name for the provided column.
public
baseColumnType(string $column) : string|null
Parameters
- $column : string
-
The column name to get the base type from
Tags
Return values
string|null —The base type name
columns()
Get the column names in the table.
public
columns() : array<string|int, string>
Tags
Return values
array<string|int, string>constraints()
Get the names of all the constraints in the table.
public
constraints() : array<string|int, string>
Tags
Return values
array<string|int, string>createSql()
Generate the SQL to create the Table.
public
createSql(Connection $connection) : array<string|int, mixed>
Parameters
- $connection : Connection
-
The connection to generate SQL for.
Tags
Return values
array<string|int, mixed> —List of SQL statements to create the table and the required indexes.
defaultValues()
Get a hash of columns and their default values.
public
defaultValues() : array<string, mixed>
Tags
Return values
array<string, mixed>dropConstraint()
Remove a constraint.
public
dropConstraint(string $name) : $this
Parameters
- $name : string
-
Name of the constraint to remove
Tags
Return values
$thisdropConstraintSql()
Generate the SQL statements to drop the constraints to the table
public
dropConstraintSql(Connection $connection) : array<string|int, mixed>
Parameters
- $connection : Connection
-
The connection to generate SQL for.
Tags
Return values
array<string|int, mixed> —SQL to drop a table.
dropSql()
Generate the SQL to drop a table.
public
dropSql(Connection $connection) : array<string|int, mixed>
Parameters
- $connection : Connection
-
The connection to generate SQL for.
Tags
Return values
array<string|int, mixed> —SQL to drop a table.
getColumn()
Get column data in the table.
public
getColumn(string $name) : array<string, mixed>|null
Parameters
- $name : string
-
The column name.
Tags
Return values
array<string, mixed>|null —Column data or null.
getColumnType()
Returns column type or null if a column does not exist.
public
getColumnType(string $name) : string|null
Parameters
- $name : string
-
The column to get the type of.
Tags
Return values
string|nullgetConstraint()
Read information about a constraint based on name.
public
getConstraint(string $name) : array<string, mixed>|null
Parameters
- $name : string
-
The name of the constraint.
Tags
Return values
array<string, mixed>|null —Array of constraint data, or null
getIndex()
Read information about an index based on name.
public
getIndex(string $name) : array<string, mixed>|null
Parameters
- $name : string
-
The name of the index.
Tags
Return values
array<string, mixed>|null —Array of index data, or null
getOptions()
Gets the options for a table.
public
getOptions() : array<string, mixed>
Tags
Return values
array<string, mixed> —An array of options.
getPrimaryKey()
Get the column(s) used for the primary key.
public
getPrimaryKey() : array<string|int, string>
Tags
Return values
array<string|int, string> —Column name(s) for the primary key. An empty list will be returned when the table has no primary key.
hasAutoincrement()
Check whether a table has an autoIncrement column defined.
public
hasAutoincrement() : bool
Return values
boolhasColumn()
Returns true if a column exists in the schema.
public
hasColumn(string $name) : bool
Parameters
- $name : string
-
Column name.
Tags
Return values
boolindexes()
Get the names of all the indexes in the table.
public
indexes() : array<string|int, string>
Tags
Return values
array<string|int, string>isNullable()
Check whether a field is nullable
public
isNullable(string $name) : bool
Parameters
- $name : string
-
The column to get the type of.
Tags
Return values
bool —Whether the field is nullable.
isTemporary()
Gets whether the table is temporary in the database.
public
isTemporary() : bool
Tags
Return values
bool —The current temporary setting.
name()
Get the name of the table.
public
name() : string
Tags
Return values
stringprimaryKey()
Get the column(s) used for the primary key.
public
primaryKey() : array<string|int, mixed>
Return values
array<string|int, mixed> —Column name(s) for the primary key. An empty list will be returned when the table has no primary key.
removeColumn()
Remove a column from the table schema.
public
removeColumn(string $name) : $this
Parameters
- $name : string
-
The name of the column
Tags
Return values
$thissetColumnType()
Sets the type of a column.
public
setColumnType(string $name, string $type) : $this
Parameters
- $name : string
-
The column to set the type of.
- $type : string
-
The type to set the column to.
Tags
Return values
$thissetOptions()
Sets the options for a table.
public
setOptions(array<string|int, mixed> $options) : $this
Parameters
- $options : array<string|int, mixed>
-
The options to set, or null to read options.
Tags
Return values
$thissetTemporary()
Sets whether the table is temporary in the database.
public
setTemporary(bool $temporary) : $this
Parameters
- $temporary : bool
-
Whether the table is to be temporary.
Tags
Return values
$thistruncateSql()
Generate the SQL statements to truncate a table
public
truncateSql(Connection $connection) : array<string|int, mixed>
Parameters
- $connection : Connection
-
The connection to generate SQL for.
Tags
Return values
array<string|int, mixed> —SQL to truncate a table.
typeMap()
Returns an array where the keys are the column names in the schema and the values the database type they have.
public
typeMap() : array<string, string>
Tags
Return values
array<string, string>_checkForeignKey()
Helper method to check/validate foreign keys.
protected
_checkForeignKey(array<string, mixed> $attrs) : array<string, mixed>
Parameters
- $attrs : array<string, mixed>
-
Attributes to set.