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 summary
-
stringACTION_CASCADE ¶'cascade' -
stringACTION_NO_ACTION ¶'noAction' -
stringACTION_RESTRICT ¶'restrict' -
stringACTION_SET_DEFAULT ¶'setDefault' -
stringACTION_SET_NULL ¶'setNull' -
stringCONSTRAINT_FOREIGN ¶'foreign' -
stringCONSTRAINT_PRIMARY ¶'primary' -
stringCONSTRAINT_UNIQUE ¶'unique' -
stringINDEX_FULLTEXT ¶'fulltext' -
stringINDEX_INDEX ¶'index' -
intLENGTH_LONG ¶4294967295 -
intLENGTH_MEDIUM ¶16777215 -
intLENGTH_TINY ¶255 -
stringTYPE_BIGINTEGER ¶'biginteger' -
stringTYPE_BINARY ¶'binary' -
stringTYPE_BINARY_UUID ¶'binaryuuid' -
stringTYPE_BOOLEAN ¶'boolean' -
stringTYPE_CHAR ¶'char' -
stringTYPE_DATE ¶'date' -
stringTYPE_DATETIME ¶'datetime' -
stringTYPE_DATETIME_FRACTIONAL ¶'datetimefractional' -
stringTYPE_DECIMAL ¶'decimal' -
stringTYPE_FLOAT ¶'float' -
stringTYPE_INTEGER ¶'integer' -
stringTYPE_JSON ¶'json' -
stringTYPE_SMALLINTEGER ¶'smallinteger' -
stringTYPE_STRING ¶'string' -
stringTYPE_TEXT ¶'text' -
stringTYPE_TIME ¶'time' -
stringTYPE_TIMESTAMP ¶'timestamp' -
stringTYPE_TIMESTAMP_FRACTIONAL ¶'timestampfractional' -
stringTYPE_TIMESTAMP_TIMEZONE ¶'timestamptimezone' -
stringTYPE_TINYINTEGER ¶'tinyinteger' -
stringTYPE_UUID ¶'uuid'
Properties summary
-
$_columnExtras protected static
arrayAdditional type specific properties.
-
$_columnKeys protected static
arrayThe valid keys that can be used in a column definition.
-
$_columns protected
arrayColumns in the table.
-
$_constraints protected
arrayConstraints in the table.
-
$_indexKeys protected static
arrayThe valid keys that can be used in an index definition.
-
$_indexes protected
arrayIndexes in the table.
-
$_options protected
arrayOptions for the table.
-
$_table protected
stringThe name of the table
-
$_temporary protected
boolWhether or not the table is temporary
-
$_typeMap protected
arrayA map with columns to types
-
$_validConstraintTypes protected static
arrayNames of the valid constraint types.
-
$_validForeignKeyActions protected static
arrayNames of the valid foreign key actions.
-
$_validIndexTypes protected static
arrayNames of the valid index types.
-
$columnLengths public static
arrayValid 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.
-
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 or not 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 or not 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
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 $columns)
Constructor.
Parameters
-
string$table The table name.
-
array$columns optional The list of columns for the schema.
_checkForeignKey() protected ¶
_checkForeignKey(array $attrs)
Helper method to check/validate foreign keys.
Parameters
-
array$attrs Attributes to set.
Returns
arrayThrows
Cake\Database\ExceptionWhen foreign key definition is not valid.
addColumn() public ¶
addColumn(string $name, mixed $attrs)
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 or not the column can hold nulls.fixedWhether or not the column is a fixed length column. This is only present/valid with string columns.unsignedWhether or not 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 The name of the column
-
string|array$attrs The attributes for the column or the type name.
Returns
$thisaddConstraint() public ¶
addConstraint(string $name, mixed $attrs)
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 The name of the constraint.
-
array|string$attrs The attributes for the constraint. If string it will be used as
type.
Returns
$thisThrows
Cake\Database\ExceptionaddConstraintSql() public ¶
addConstraintSql(\Cake\Database\Connection $connection)
Generate the SQL statements to add the constraints to the table
Parameters
-
\Cake\Database\Connection$connection The connection to generate SQL for.
Returns
arraySQL to add the constraints.
addIndex() public ¶
addIndex(string $name, mixed $attrs)
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 The name of the index.
-
array|string$attrs The attributes for the index. If string it will be used as
type.
Returns
$thisThrows
Cake\Database\ExceptionbaseColumnType() public ¶
baseColumnType(string $column)
Returns the base type name for the provided column.
This represent the database type a more complex class is based upon.
Parameters
-
string$column The column name to get the base type from
Returns
string|nullThe base type name
constraints() public ¶
constraints()
Get the names of all the constraints in the table.
Returns
string[]createSql() public ¶
createSql(\Cake\Database\Connection $connection)
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 The connection to generate SQL for.
Returns
arrayList of SQL statements to create the table and the required indexes.
defaultValues() public ¶
defaultValues()
Get a hash of columns and their default values.
Returns
arraydropConstraint() public ¶
dropConstraint(string $name)
Remove a constraint.
Parameters
-
string$name Name of the constraint to remove
Returns
$thisdropConstraintSql() public ¶
dropConstraintSql(\Cake\Database\Connection $connection)
Generate the SQL statements to drop the constraints to the table
Parameters
-
\Cake\Database\Connection$connection The connection to generate SQL for.
Returns
arraySQL to drop a table.
dropSql() public ¶
dropSql(\Cake\Database\Connection $connection)
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 The connection to generate SQL for.
Returns
arraySQL to drop a table.
getColumn() public ¶
getColumn(string $name)
Get column data in the table.
Parameters
-
string$name The column name.
Returns
array|nullColumn data or null.
getColumnType() public ¶
getColumnType(string $name)
Returns column type or null if a column does not exist.
Parameters
-
string$name The column to get the type of.
Returns
string|nullgetConstraint() public ¶
getConstraint(string $name)
Read information about a constraint based on name.
Parameters
-
string$name The name of the constraint.
Returns
array|nullArray of constraint data, or null
getIndex() public ¶
getIndex(string $name)
Read information about an index based on name.
Parameters
-
string$name The name of the index.
Returns
array|nullArray of index data, or null
getOptions() public ¶
getOptions()
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
arrayAn array of options.
getPrimaryKey() public ¶
getPrimaryKey()
Get the column(s) used for the primary key.
Returns
arrayColumn name(s) for the primary key. An empty list will be returned when the table has no primary key.
hasAutoincrement() public ¶
hasAutoincrement()
Check whether or not a table has an autoIncrement column defined.
Returns
boolhasColumn() public ¶
hasColumn(string $name)
Returns true if a column exists in the schema.
Parameters
-
string$name Column name.
Returns
boolisNullable() public ¶
isNullable(string $name)
Check whether or not a field is nullable
Missing columns are nullable.
Parameters
-
string$name The column to get the type of.
Returns
boolWhether or not the field is nullable.
isTemporary() public ¶
isTemporary()
Gets whether the table is temporary in the database.
Returns
boolThe current temporary setting.
primaryKey() public ¶
primaryKey()
Get the column(s) used for the primary key.
Returns
arrayColumn name(s) for the primary key. An empty list will be returned when the table has no primary key.
removeColumn() public ¶
removeColumn(string $name)
Remove a column from the table schema.
If the column is not defined in the table, no error will be raised.
Parameters
-
string$name The name of the column
Returns
$thissetColumnType() public ¶
setColumnType(string $name, string $type)
Sets the type of a column.
Parameters
-
string$name The column to set the type of.
-
string$type The type to set the column to.
Returns
$thissetOptions() public ¶
setOptions(array $options)
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$options The options to set, or null to read options.
Returns
$thissetTemporary() public ¶
setTemporary(bool $temporary)
Sets whether the table is temporary in the database.
Parameters
-
bool$temporary Whether or not the table is to be temporary.
Returns
$thistruncateSql() public ¶
truncateSql(\Cake\Database\Connection $connection)
Generate the SQL statements to truncate a table
Parameters
-
\Cake\Database\Connection$connection The connection to generate SQL for.
Returns
arraySQL to truncate a table.
typeMap() public ¶
typeMap()
Returns an array where the keys are the column names in the schema and the values the database type they have.
Returns
arrayProperty Detail
$columnLengths ¶ public static
Valid column length that can be used with text type columns
Type
array