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
-
string
'cascade'
-
string
'noAction'
-
string
'restrict'
-
string
'setDefault'
-
string
'setNull'
-
string
'foreign'
-
string
'primary'
-
string
'unique'
-
string
'fulltext'
-
string
'index'
-
integer
4294967295
-
integer
16777215
-
integer
255
Inherited Constants
-
TYPE_BIGINTEGER
,TYPE_BINARY
,TYPE_BOOLEAN
,TYPE_DATE
,TYPE_DATETIME
,TYPE_DECIMAL
,TYPE_FLOAT
,TYPE_INTEGER
,TYPE_JSON
,TYPE_SMALLINTEGER
,TYPE_STRING
,TYPE_TEXT
,TYPE_TIME
,TYPE_TIMESTAMP
,TYPE_TINYINTEGER
,TYPE_UUID
Properties summary
- Additional type specific properties.
array
-
array
The valid keys that can be used in a column definition.
-
$_columns
protectedColumns in the table.array
-
$_constraints
protectedConstraints in the table.array
-
array
The valid keys that can be used in an index definition.
-
$_indexes
protectedIndexes in the table.array
-
$_options
protectedOptions for the table.array
-
$_table
protectedThe name of the tablestring
-
$_temporary
protectedWhether or not the table is temporaryboolean
-
$_typeMap
protectedA map with columns to typesarray
- Names of the valid constraint types.
array
- Names of the valid foreign key actions.
array
- Names of the valid index types.
array
- Valid column length that can be used with text type columns
array
Method Summary
- __construct() publicConstructor.
- _checkForeignKey() protectedHelper method to check/validate foreign keys.
- addColumn() publicAdd a column to the table.
- addConstraint() publicAdd a constraint.
- addConstraintSql() publicGenerate the SQL statements to add the constraints to the table
- addIndex() publicAdd 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() publicGet column data in the table.
- columnType() public
Sets the type of a column, or returns its current type if none is passed.
- columns() publicGet the column names in the table.
- constraint() publicRead information about a constraint based on name.
- constraints() publicGet the names of all the constraints in the table.
- createSql() publicGenerate the SQL to create the Table.
- defaultValues() publicGet a hash of columns and their default values.
- dropConstraint() publicRemove a constraint.
- dropConstraintSql() publicGenerate the SQL statements to drop the constraints to the table
- dropSql() publicGenerate the SQL to drop a table.
- getColumn() publicGet column data in the table.
- getColumnType() publicReturns column type or null if a column does not exist.
- getConstraint() publicRead information about a constraint based on name.
- getIndex() publicRead information about an index based on name.
- getOptions() publicGets the options for a table.
- hasAutoincrement() publicCheck whether or not a table has an autoIncrement column defined.
- hasColumn() publicReturns true if a column exists in the schema.
- index() publicRead information about an index based on name.
- indexes() publicGet the names of all the indexes in the table.
- isNullable() publicCheck whether or not a field is nullable
- isTemporary() publicGets whether the table is temporary in the database.
- name() publicGet the name of the table.
- options() publicGet/set the options for a table.
- primaryKey() publicGet the column(s) used for the primary key.
- removeColumn() publicRemove a column from the table schema.
- setColumnType() publicSets the type of a column.
- setOptions() publicSets the options for a table.
- setTemporary() publicSets whether the table is temporary in the database.
- temporary() publicGet/Set whether the table is temporary in the database
- truncateSql() publicGenerate 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()source public
__construct( string $table , array $columns [] )
Constructor.
Parameters
- string
$table
- The table name.
- array
$columns
optional [] - The list of columns for the schema.
_checkForeignKey()source protected
_checkForeignKey( array $attrs )
Helper method to check/validate foreign keys.
Parameters
- array
$attrs
- Attributes to set.
Returns
arrayThrows
Cake\Database\Exception
When foreign key definition is not valid.
addColumn()source public
addColumn( string $name , array $attrs )
Add a column to the table.
Parameters
- string
$name
- The name of the column
- array
$attrs
- The attributes for the column.
Returns
$this
Implementation of
Cake\Datasource\SchemaInterface::addColumn()
addConstraint()source 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
Cake\Database\Exception
Implementation of
Cake\Database\Schema\TableSchemaInterface::addConstraint()
addConstraintSql()source 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.
Implementation of
Cake\Database\Schema\SqlGeneratorInterface::addConstraintSql()
addIndex()source 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
Cake\Database\Exception
Implementation of
Cake\Database\Schema\TableSchemaInterface::addIndex()
baseColumnType()source 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
Implementation of
Cake\Datasource\SchemaInterface::baseColumnType()
column()source public
column( string $name )
Get column data in the table.
Deprecated
3.5.0 Use getColumn() instead.Parameters
- string
$name
- The column name.
Returns
array|nullColumn data or null.
columnType()source public
columnType( string $name , string|null $type null )
Sets the type of a column, or returns its current type if none is passed.
Deprecated
3.5.0 Use setColumnType()/getColumnType() instead.Parameters
- string
$name
- The column to get the type of.
- string|null
$type
optional null - The type to set the column to.
Returns
string|nullEither the column type or null.
columns()source public
columns( )
Get the column names in the table.
Returns
arrayImplementation of
Cake\Datasource\SchemaInterface::columns()
constraint()source public
constraint( string $name )
Read information about a constraint based on name.
Deprecated
3.5.0 Use getConstraint() instead.Parameters
- string
$name
- The name of the constraint.
Returns
array|nullArray of constraint data, or null
constraints()source public
constraints( )
Get the names of all the constraints in the table.
Returns
arrayImplementation of
Cake\Database\Schema\TableSchemaInterface::constraints()
createSql()source 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
arrayList of SQL statements to create the table and the required indexes.
Implementation of
Cake\Database\Schema\SqlGeneratorInterface::createSql()
defaultValues()source public
defaultValues( )
Get a hash of columns and their default values.
Returns
arrayImplementation of
Cake\Datasource\SchemaInterface::defaultValues()
dropConstraint()source public
dropConstraint( string $name )
Remove a constraint.
Parameters
- string
$name
- Name of the constraint to remove
Returns
$this
Implementation of
Cake\Database\Schema\TableSchemaInterface::dropConstraint()
dropConstraintSql()source 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.
Implementation of
Cake\Database\Schema\SqlGeneratorInterface::dropConstraintSql()
dropSql()source 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
arraySQL to drop a table.
Implementation of
Cake\Database\Schema\SqlGeneratorInterface::dropSql()
getColumn()source public
getColumn( string $name )
Get column data in the table.
Parameters
- string
$name
- The column name.
Returns
array|nullColumn data or null.
Implementation of
Cake\Datasource\SchemaInterface::getColumn()
getColumnType()source 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|nullImplementation of
Cake\Datasource\SchemaInterface::getColumnType()
getConstraint()source 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
Implementation of
Cake\Database\Schema\TableSchemaInterface::getConstraint()
getIndex()source 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
Implementation of
Cake\Database\Schema\TableSchemaInterface::getIndex()
getOptions()source public
getOptions( )
Gets the options for a table.
Returns
arrayAn array of options.
Implementation of
Cake\Datasource\SchemaInterface::getOptions()
hasAutoincrement()source public
hasAutoincrement( )
Check whether or not a table has an autoIncrement column defined.
Returns
booleanImplementation of
Cake\Database\Schema\TableSchemaInterface::hasAutoincrement()
hasColumn()source public
hasColumn( string $name )
Returns true if a column exists in the schema.
Parameters
- string
$name
- Column name.
Returns
booleanImplementation of
Cake\Datasource\SchemaInterface::hasColumn()
index()source public
index( string $name )
Read information about an index based on name.
Deprecated
3.5.0 Use getIndex() instead.Parameters
- string
$name
- The name of the index.
Returns
array|nullArray of index data, or null
indexes()source public
indexes( )
Get the names of all the indexes in the table.
Returns
arrayImplementation of
Cake\Database\Schema\TableSchemaInterface::indexes()
isNullable()source public
isNullable( string $name )
Check whether or not a field is nullable
Parameters
- string
$name
- The column to get the type of.
Returns
booleanWhether or not the field is nullable.
Implementation of
Cake\Datasource\SchemaInterface::isNullable()
isTemporary()source public
isTemporary( )
Gets whether the table is temporary in the database.
Returns
booleanThe current temporary setting.
Implementation of
Cake\Database\Schema\TableSchemaInterface::isTemporary()
name()source public
name( )
Get the name of the table.
Returns
stringImplementation of
Cake\Datasource\SchemaInterface::name()
options()source 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
3.4.0 Use setOptions()/getOptions() instead.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()source 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.
Implementation of
Cake\Database\Schema\TableSchemaInterface::primaryKey()
removeColumn()source public
removeColumn( string $name )
Remove a column from the table schema.
Parameters
- string
$name
- The name of the column
Returns
$this
Implementation of
Cake\Datasource\SchemaInterface::removeColumn()
setColumnType()source 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
Cake\Datasource\SchemaInterface::setColumnType()
setOptions()source 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
Cake\Datasource\SchemaInterface::setOptions()
setTemporary()source 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
Cake\Database\Schema\TableSchemaInterface::setTemporary()
temporary()source public
temporary( boolean|null $temporary null )
Get/Set whether the table is temporary in the database
Deprecated
3.4.0 Use setTemporary()/isTemporary() instead.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()source 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.
Implementation of
Cake\Database\Schema\SqlGeneratorInterface::truncateSql()
typeMap()source public
typeMap( )
Returns an array where the keys are the column names in the schema and the values the database type they have.
Returns
arrayImplementation of
Cake\Datasource\SchemaInterface::typeMap()
Properties detail
$_columnExtrassource
protected static array
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, ], ]
$_columnKeyssource
protected static array
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, ]
$_indexKeyssource
protected static array
The valid keys that can be used in an index definition.
[ 'type' => null, 'columns' => [], 'length' => [], 'references' => [], 'update' => 'restrict', 'delete' => 'restrict', ]
$_validConstraintTypessource
protected static array
Names of the valid constraint types.
[ self::CONSTRAINT_PRIMARY, self::CONSTRAINT_UNIQUE, self::CONSTRAINT_FOREIGN, ]
$_validForeignKeyActionssource
protected static array
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, ]
$_validIndexTypessource
protected static array
Names of the valid index types.
[ self::INDEX_INDEX, self::INDEX_FULLTEXT, ]
$columnLengthssource
public static array
Valid column length that can be used with text type columns
[ 'tiny' => self::LENGTH_TINY, 'medium' => self::LENGTH_MEDIUM, 'long' => self::LENGTH_LONG ]
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Database.Schema.TableSchema.html