Class TableSchema
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.
Constants
-
stringACTION_CASCADE ¶'cascade'Foreign key cascade action
-
stringACTION_NO_ACTION ¶'noAction'Foreign key no action
-
stringACTION_RESTRICT ¶'restrict'Foreign key restrict action
-
stringACTION_SET_DEFAULT ¶'setDefault'Foreign key restrict default
-
stringACTION_SET_NULL ¶'setNull'Foreign key set null action
-
stringCONSTRAINT_FOREIGN ¶'foreign'Foreign constraint type
-
stringCONSTRAINT_PRIMARY ¶'primary'Primary constraint type
-
stringCONSTRAINT_UNIQUE ¶'unique'Unique constraint type
-
stringINDEX_FULLTEXT ¶'fulltext'Fulltext index type
-
stringINDEX_INDEX ¶'index'Index - index type
-
intLENGTH_LONG ¶4294967295Column length when using a
longcolumn type -
intLENGTH_MEDIUM ¶16777215Column length when using a
mediumcolumn type -
intLENGTH_TINY ¶255Column length when using a
tinycolumn type -
stringTYPE_BIGINTEGER ¶'biginteger'Big Integer column type
-
stringTYPE_BINARY ¶'binary'Binary column type
-
stringTYPE_BINARY_UUID ¶'binaryuuid'Binary UUID column type
-
stringTYPE_BOOLEAN ¶'boolean'Boolean column type
-
stringTYPE_CHAR ¶'char'Char column type
-
stringTYPE_DATE ¶'date'Date column type
-
stringTYPE_DATETIME ¶'datetime'Datetime column type
-
stringTYPE_DATETIME_FRACTIONAL ¶'datetimefractional'Datetime with fractional seconds column type
-
stringTYPE_DECIMAL ¶'decimal'Decimal column type
-
stringTYPE_FLOAT ¶'float'Float column type
-
stringTYPE_INTEGER ¶'integer'Integer column type
-
stringTYPE_JSON ¶'json'JSON column type
-
stringTYPE_SMALLINTEGER ¶'smallinteger'Small Integer column type
-
stringTYPE_STRING ¶'string'String column type
-
stringTYPE_TEXT ¶'text'Text column type
-
stringTYPE_TIME ¶'time'Time column type
-
stringTYPE_TIMESTAMP ¶'timestamp'Timestamp column type
-
stringTYPE_TIMESTAMP_FRACTIONAL ¶'timestampfractional'Timestamp with fractional seconds column type
-
stringTYPE_TIMESTAMP_TIMEZONE ¶'timestamptimezone'Timestamp with time zone column type
-
stringTYPE_TINYINTEGER ¶'tinyinteger'Tiny Integer column type
-
stringTYPE_UUID ¶'uuid'UUID column type
Property Summary
-
$_columnExtras protected static
array<string, array<string, mixed>>Additional type specific properties.
-
$_columnKeys protected static
array<string, mixed>The valid keys that can be used in a column definition.
-
$_columns protected
array<string, array>Columns in the table.
-
$_constraints protected
array<string, array<string, mixed>>Constraints in the table.
-
$_indexKeys protected static
array<string, mixed>The valid keys that can be used in an index definition.
-
$_indexes protected
array<string, array>Indexes in the table.
-
$_options protected
array<string, mixed>Options for the table.
-
$_table protected
stringThe name of the table
-
$_temporary protected
boolWhether the table is temporary
-
$_typeMap protected
array<string, string>A map with columns to types
-
$_validConstraintTypes protected static
array<string>Names of the valid constraint types.
-
$_validForeignKeyActions protected static
array<string>Names of the valid foreign key actions.
-
$_validIndexTypes protected static
array<string>Names of the valid index types.
-
$columnLengths public static
array<string, int>Valid column length that can be used with text type columns
Method Summary
-
__construct() public
Constructor.
-
__debugInfo() public
Returns an array of the table schema.
-
_checkForeignKey() protected
Helper method to check/validate foreign keys.
-
addColumn() public
Add a column to the table.
-
addConstraint() public
Add a constraint.
-
addConstraintSql() public
Generate the SQL statements to add the constraints to the table
-
addIndex() public
Add an index.
-
baseColumnType() public
Returns the base type name for the provided column. This represent the database type a more complex class is based upon.
-
columns() public
Get the column names in the table.
-
constraints() public
Get the names of all the constraints in the table.
-
createSql() public
Generate the SQL to create the Table.
-
defaultValues() public
Get a hash of columns and their default values.
-
dropConstraint() public
Remove a constraint.
-
dropConstraintSql() public
Generate the SQL statements to drop the constraints to the table
-
dropSql() public
Generate the SQL to drop a table.
-
getColumn() public
Get column data in the table.
-
getColumnType() public
Returns column type or null if a column does not exist.
-
getConstraint() public
Read information about a constraint based on name.
-
getIndex() public
Read information about an index based on name.
-
getOptions() public
Gets the options for a table.
-
getPrimaryKey() public
Get the column(s) used for the primary key.
-
hasAutoincrement() public
Check whether a table has an autoIncrement column defined.
-
hasColumn() public
Returns true if a column exists in the schema.
-
indexes() public
Get the names of all the indexes in the table.
-
isNullable() public
Check whether a field is nullable
-
isTemporary() public
Gets whether the table is temporary in the database.
-
name() public
Get the name of the table.
-
primaryKey() public deprecated
Get the column(s) used for the primary key.
-
removeColumn() public
Remove a column from the table schema.
-
setColumnType() public
Sets the type of a column.
-
setOptions() public
Sets the options for a table.
-
setTemporary() public
Sets whether the table is temporary in the database.
-
truncateSql() public
Generate the SQL statements to truncate a table
-
typeMap() public
Returns an array where the keys are the column names in the schema and the values the database type they have.
Method Detail
__construct() ¶ public
__construct(string $table, array<string, array|string> $columns = [])
Constructor.
Parameters
-
string$table The table name.
-
array<string, array|string>$columns optional The list of columns for the schema.
__debugInfo() ¶ public
__debugInfo(): array<string, mixed>
Returns an array of the table schema.
Returns
array<string, mixed>_checkForeignKey() ¶ protected
_checkForeignKey(array<string, mixed> $attrs): array<string, mixed>
Helper method to check/validate foreign keys.
Parameters
-
array<string, mixed>$attrs Attributes to set.
Returns
array<string, mixed>Throws
Cake\Database\Exception\DatabaseExceptionWhen foreign key definition is not valid.
addColumn() ¶ public
addColumn(string $name, array<string, mixed>|string $attrs): $this
Add a column to the table.
Attributes
Columns can have several attributes:
typeThe type of the column. This should be one of CakePHP's abstract types.lengthThe length of the column.precisionThe number of decimal places to store for float and decimal types.defaultThe default value of the column.nullWhether the column can hold nulls.fixedWhether the column is a fixed length column. This is only present/valid with string columns.unsignedWhether the column is an unsigned column. This is only present/valid for integer, decimal, float columns.
In addition to the above keys, the following keys are implemented in some database dialects, but not all:
commentThe comment for the column.
Parameters
-
string$name -
array<string, mixed>|string$attrs
Returns
$thisaddConstraint() ¶ public
addConstraint(string $name, array<string, mixed>|string $attrs): $this
Add a constraint.
Used to add constraints to a table. For example primary keys, unique keys and foreign keys.
Attributes
typeThe type of constraint being added.columnsThe columns in the index.referencesThe table, column a foreign key references.updateThe behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.deleteThe behavior on delete. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
The default for 'update' & 'delete' is 'cascade'.
Parameters
-
string$name -
array<string, mixed>|string$attrs
Returns
$thisaddConstraintSql() ¶ public
addConstraintSql(Cake\Database\Connection $connection): array
Generate the SQL statements to add the constraints to the table
Parameters
-
Cake\Database\Connection$connection
Returns
arrayaddIndex() ¶ public
addIndex(string $name, array<string, mixed>|string $attrs): $this
Add an index.
Used to add indexes, and full text indexes in platforms that support them.
Attributes
typeThe type of index being added.columnsThe columns in the index.
Parameters
-
string$name -
array<string, mixed>|string$attrs
Returns
$thisbaseColumnType() ¶ public
baseColumnType(string $column): string|null
Returns the base type name for the provided column. This represent the database type a more complex class is based upon.
Parameters
-
string$column
Returns
string|nullcolumns() ¶ public
columns(): array<string>
Get the column names in the table.
Returns
array<string>constraints() ¶ public
constraints(): array<string>
Get the names of all the constraints in the table.
Returns
array<string>createSql() ¶ public
createSql(Cake\Database\Connection $connection): array
Generate the SQL to create the Table.
Uses the connection to access the schema dialect to generate platform specific SQL.
Parameters
-
Cake\Database\Connection$connection
Returns
arraydefaultValues() ¶ public
defaultValues(): array<string, mixed>
Get a hash of columns and their default values.
Returns
array<string, mixed>dropConstraint() ¶ public
dropConstraint(string $name): $this
Remove a constraint.
Parameters
-
string$name
Returns
$thisdropConstraintSql() ¶ public
dropConstraintSql(Cake\Database\Connection $connection): array
Generate the SQL statements to drop the constraints to the table
Parameters
-
Cake\Database\Connection$connection
Returns
arraydropSql() ¶ public
dropSql(Cake\Database\Connection $connection): array
Generate the SQL to drop a table.
Uses the connection to access the schema dialect to generate platform specific SQL.
Parameters
-
Cake\Database\Connection$connection
Returns
arraygetColumn() ¶ public
getColumn(string $name): array<string, mixed>|null
Get column data in the table.
Parameters
-
string$name
Returns
array<string, mixed>|nullgetColumnType() ¶ public
getColumnType(string $name): string|null
Returns column type or null if a column does not exist.
Parameters
-
string$name
Returns
string|nullgetConstraint() ¶ public
getConstraint(string $name): array<string, mixed>|null
Read information about a constraint based on name.
Parameters
-
string$name
Returns
array<string, mixed>|nullgetIndex() ¶ public
getIndex(string $name): array<string, mixed>|null
Read information about an index based on name.
Parameters
-
string$name
Returns
array<string, mixed>|nullgetOptions() ¶ public
getOptions(): array<string, mixed>
Gets the options for a table.
Table options allow you to set platform specific table level options. For example the engine type in MySQL.
Returns
array<string, mixed>getPrimaryKey() ¶ public
getPrimaryKey(): array<string>
Get the column(s) used for the primary key.
Returns
array<string>hasAutoincrement() ¶ public
hasAutoincrement(): bool
Check whether a table has an autoIncrement column defined.
Returns
boolhasColumn() ¶ public
hasColumn(string $name): bool
Returns true if a column exists in the schema.
Parameters
-
string$name
Returns
boolindexes() ¶ public
indexes(): array<string>
Get the names of all the indexes in the table.
Returns
array<string>isNullable() ¶ public
isNullable(string $name): bool
Check whether a field is nullable
Missing columns are nullable.
Parameters
-
string$name
Returns
boolisTemporary() ¶ public
isTemporary(): bool
Gets whether the table is temporary in the database.
Returns
boolremoveColumn() ¶ public
removeColumn(string $name): $this
Remove a column from the table schema.
If the column is not defined in the table, no error will be raised.
Parameters
-
string$name
Returns
$thissetColumnType() ¶ public
setColumnType(string $name, string $type): $this
Sets the type of a column.
Parameters
-
string$name -
string$type
Returns
$thissetOptions() ¶ public
setOptions(array<string, mixed> $options): $this
Sets the options for a table.
Table options allow you to set platform specific table level options. For example the engine type in MySQL.
Parameters
-
array<string, mixed>$options
Returns
$thissetTemporary() ¶ public
setTemporary(bool $temporary): $this
Sets whether the table is temporary in the database.
Parameters
-
bool$temporary
Returns
$thistruncateSql() ¶ public
truncateSql(Cake\Database\Connection $connection): array
Generate the SQL statements to truncate a table
Parameters
-
Cake\Database\Connection$connection
Returns
arraytypeMap() ¶ public
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.
Returns
array<string, string>Property Detail
$_columnExtras ¶ protected static
Additional type specific properties.
Type
array<string, array<string, mixed>>$_columnKeys ¶ protected static
The valid keys that can be used in a column definition.
Type
array<string, mixed>$_indexKeys ¶ protected static
The valid keys that can be used in an index definition.
Type
array<string, mixed>$_validForeignKeyActions ¶ protected static
Names of the valid foreign key actions.
Type
array<string>$columnLengths ¶ public static
Valid column length that can be used with text type columns
Type
array<string, int>