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.
- Cake\Database\Schema\TableSchema implements Cake\Database\Schema\TableSchemaInterface, Cake\Database\Schema\SqlGeneratorInterface
Constants summary
Inherited Constants
Properties summary
-
$_columnExtras
protected staticarray
Additional type specific properties. -
$_columnKeys
protected staticarray
The valid keys that can be used in a column definition.
-
$_columns
protectedarray
Columns in the table. -
$_constraints
protectedarray
Constraints in the table. -
$_indexKeys
protected staticarray
The valid keys that can be used in an index definition.
-
$_indexes
protectedarray
Indexes in the table. -
$_options
protectedarray
Options for the table. -
$_table
protectedstring
The name of the table -
$_temporary
protectedboolean
Whether or not the table is temporary -
$_typeMap
protectedarray
A map with columns to types -
$_validConstraintTypes
protected staticarray
Names of the valid constraint types. -
$_validForeignKeyActions
protected staticarray
Names of the valid foreign key actions. -
$_validIndexTypes
protected staticarray
Names of the valid index types. -
$columnLengths
public staticarray
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.
-
column() public
Get column data in the table. -
columnType() public
Sets the type of a column, or returns its current type if none is passed.
-
columns() public
Get the column names in the table. -
constraint() public
Read information about a constraint based on name. -
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. -
hasAutoincrement() public
Check whether or not a table has an autoIncrement column defined. -
hasColumn() public
Returns true if a column exists in the schema. -
index() public
Read information about an index based on name. -
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. -
options() public
Get/set the options for a 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. -
temporary() public
Get/Set 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
Throws
addColumn() public ¶
addColumn( string $name , array|string $attrs )
Add a column to the table.
Parameters
- string $name
- The name of the column
- array|string $attrs
- The attributes for the column.
Returns
$this
Implementation of
addConstraint() public ¶
addConstraint( string $name , array $attrs )
Add a constraint.
Parameters
- string $name
- The name of the constraint.
- array $attrs
- The attributes for the constraint.
Returns
$this
Throws
Implementation of
addConstraintSql() 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
SQL to add the constraints.
Implementation of
addIndex() public ¶
addIndex( string $name , array $attrs )
Add an index.
Parameters
- string $name
- The name of the index.
- array $attrs
- The attributes for the index.
Returns
$this
Throws
Implementation of
baseColumnType() 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
The base type name
Implementation of
column() public ¶
column( string $name )
Get column data in the table.
Deprecated
Parameters
- string $name
- The column name.
Returns
Column data or null.
columnType() public ¶
columnType( string $name , string|null $type null )
Sets the type of a column, or returns its current type if none is passed.
Deprecated
Parameters
- string $name
- The column to get the type of.
- string|null $type optional null
- The type to set the column to.
Returns
Either the column type or null.
constraint() public ¶
constraint( string $name )
Read information about a constraint based on name.
Deprecated
Parameters
- string $name
- The name of the constraint.
Returns
Array of constraint data, or null
constraints() public ¶
constraints( )
Get the names of all the constraints in the table.
Returns
Implementation of
createSql() public ¶
createSql( Cake\Database\Connection
$connection )
Generate the SQL to create the Table.
Parameters
-
Cake\Database\Connection
$connection - The connection to generate SQL for.
Returns
List of SQL statements to create the table and the required indexes.
Implementation of
defaultValues() public ¶
defaultValues( )
Get a hash of columns and their default values.
Returns
Implementation of
dropConstraint() public ¶
dropConstraint( string $name )
Remove a constraint.
Parameters
- string $name
- Name of the constraint to remove
Returns
$this
Implementation of
dropConstraintSql() 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
SQL to drop a table.
Implementation of
dropSql() public ¶
dropSql( Cake\Database\Connection
$connection )
Generate the SQL to drop a table.
Parameters
-
Cake\Database\Connection
$connection - The connection to generate SQL for.
Returns
SQL to drop a table.
Implementation of
getColumn() public ¶
getColumn( string $name )
Get column data in the table.
Parameters
- string $name
- The column name.
Returns
Column data or null.
Implementation of
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
Implementation of
getConstraint() public ¶
getConstraint( string $name )
Read information about a constraint based on name.
Parameters
- string $name
- The name of the constraint.
Returns
Array of constraint data, or null
Implementation of
getIndex() public ¶
getIndex( string $name )
Read information about an index based on name.
Parameters
- string $name
- The name of the index.
Returns
Array of index data, or null
Implementation of
getOptions() public ¶
getOptions( )
Gets the options for a table.
Returns
An array of options.
Implementation of
hasAutoincrement() public ¶
hasAutoincrement( )
Check whether or not a table has an autoIncrement column defined.
Returns
Implementation of
hasColumn() public ¶
hasColumn( string $name )
Returns true if a column exists in the schema.
Parameters
- string $name
- Column name.
Returns
Implementation of
index() public ¶
index( string $name )
Read information about an index based on name.
Deprecated
Parameters
- string $name
- The name of the index.
Returns
Array of index data, or null
indexes() public ¶
indexes( )
Get the names of all the indexes in the table.
Returns
Implementation of
isNullable() public ¶
isNullable( string $name )
Check whether or not a field is nullable
Parameters
- string $name
- The column to get the type of.
Returns
Whether or not the field is nullable.
Implementation of
isTemporary() public ¶
isTemporary( )
Gets whether the table is temporary in the database.
Returns
The current temporary setting.
Implementation of
options() public ¶
options( array|null $options null )
Get/set the options for a table.
Table options allow you to set platform specific table level options. For example the engine type in MySQL.
Deprecated
Parameters
- array|null $options optional null
- The options to set, or null to read options.
Returns
$this|array Either the TableSchema instance, or an array of options when reading.
primaryKey() public ¶
primaryKey( )
Get the column(s) used for the primary key.
Returns
Column name(s) for the primary key. An empty list will be returned when the table has no primary key.
Implementation of
removeColumn() public ¶
removeColumn( string $name )
Remove a column from the table schema.
Parameters
- string $name
- The name of the column
Returns
$this
Implementation of
setColumnType() 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
$this
Implementation of
setOptions() public ¶
setOptions( array $options )
Sets the options for a table.
Parameters
- array $options
- The options to set, or null to read options.
Returns
$this
Implementation of
setTemporary() public ¶
setTemporary( boolean $temporary )
Sets whether the table is temporary in the database.
Parameters
- boolean $temporary
- Whether or not the table is to be temporary.
Returns
$this
Implementation of
temporary() public ¶
temporary( boolean|null $temporary null )
Get/Set whether the table is temporary in the database
Deprecated
Parameters
- boolean|null $temporary optional null
- whether or not the table is to be temporary
Returns
$this|bool Either the TableSchema instance, the current temporary setting
truncateSql() 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
SQL to truncate a table.
Implementation of
Properties detail
$_columnExtras ¶
Additional type specific properties.
[ 'string' => [ 'fixed' => null, '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.
[ 'type' => null, 'baseType' => null, 'length' => null, 'precision' => null, 'null' => null, 'default' => null, 'comment' => null, ]
$_indexKeys ¶
The valid keys that can be used in an index definition.
[ 'type' => null, 'columns' => [], 'length' => [], 'references' => [], 'update' => 'restrict', 'delete' => 'restrict', ]
$_validConstraintTypes ¶
Names of the valid constraint types.
[ self::CONSTRAINT_PRIMARY, self::CONSTRAINT_UNIQUE, self::CONSTRAINT_FOREIGN, ]
$_validForeignKeyActions ¶
Names of the valid foreign key actions.
[ 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.
[ self::INDEX_INDEX, self::INDEX_FULLTEXT, ]
$columnLengths ¶
Valid column length that can be used with text type columns
[ 'tiny' => self::LENGTH_TINY, 'medium' => self::LENGTH_MEDIUM, 'long' => self::LENGTH_LONG ]