W3cubDocs

/CakePHP 3.4

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

Properties summary

  • $_columnExtras protected static
    array
    Additional type specific properties.
  • $_columnKeys protected static
    array

    The valid keys that can be used in a column definition.

  • $_columns protected
    array
    Columns in the table.
  • $_constraints protected
    array
    Constraints in the table.
  • $_indexKeys protected static
    array

    The valid keys that can be used in an index definition.

  • $_indexes protected
    array
    Indexes in the table.
  • $_options protected
    array
    Options for the table.
  • $_table protected
    string
    The name of the table
  • $_temporary protected
    boolean
    Whether or not the table is temporary
  • $_typeMap protected
    array
    A map with columns to types
  • $_validConstraintTypes protected static
    array
    Names of the valid constraint types.
  • $_validForeignKeyActions protected static
    array
    Names of the valid foreign key actions.
  • $_validIndexTypes protected static
    array
    Names of the valid index types.
  • $columnLengths public static
    array
    Valid column length that can be used with text type columns

Method Summary

  • __construct() public
    Constructor.
  • _checkForeignKey() protected
    Helper method to check/validate foreign keys.
  • addColumn() public
    Add a column to the table.
  • Add a constraint.
  • Generate the SQL statements to add the constraints to the table
  • addIndex() public
    Add an index.
  • 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.
  • Get a hash of columns and their default values.
  • Remove a constraint.
  • Generate the SQL statements to drop the constraints to the table
  • dropSql() public
    Generate the SQL to drop a table.
  • getOptions() public
    Gets the options for a table.
  • Check whether or not a table has an autoIncrement column defined.
  • 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.
  • setOptions() public
    Sets the options for a table.
  • 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()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

array

Throws

Cake\Database\Exception
When foreign key definition is not valid.

addColumn()source public

addColumn( string $name , array $attrs )

Add a column to the table.

Attributes

Columns can have several attributes:

  • type The type of the column. This should be one of CakePHP's abstract types.
  • length The length of the column.
  • precision The number of decimal places to store for float and decimal types.
  • default The default value of the column.
  • null Whether or not the column can hold nulls.
  • fixed Whether or not the column is a fixed length column. This is only present/valid with string columns.
  • unsigned Whether 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:

  • comment The comment for the column.

Parameters

string $name
The name of the column
array $attrs
The attributes for the column.

Returns


$this

addConstraint()source public

addConstraint( string $name , array $attrs )

Add a constraint.

Used to add constraints to a table. For example primary keys, unique keys and foreign keys.

Attributes

  • type The type of constraint being added.
  • columns The columns in the index.
  • references The table, column a foreign key references.
  • update The behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
  • delete The 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 $attrs
The attributes for the constraint.

Returns


$this

Throws

Cake\Database\Exception

addConstraintSql()source public

addConstraintSql( Cake\Datasource\ConnectionInterface $connection )

Generate the SQL statements to add the constraints to the table

Parameters

Cake\Datasource\ConnectionInterface $connection
The connection to generate SQL for.

Returns

array
SQL to drop a table.

addIndex()source public

addIndex( string $name , array $attrs )

Add an index.

Used to add indexes, and full text indexes in platforms that support them.

Attributes

  • type The type of index being added.
  • columns The columns in the index.

Parameters

string $name
The name of the index.
array $attrs
The attributes for the index.

Returns


$this

Throws

Cake\Database\Exception

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|null
The base type name

column()source public

column( string $name )

Get column data in the table.

Parameters

string $name
The column name.

Returns

array|null
Column 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.

Parameters

string $name
The column to get the type of.
string|null $type optional null
The type to set the column to.

Returns

string|null
Either the column type or null.

columns()source public

columns( )

Get the column names in the table.

Returns

array

constraint()source public

constraint( string $name )

Read information about a constraint based on name.

Parameters

string $name
The name of the constraint.

Returns

array|null
Array of constraint data, or null

constraints()source public

constraints( )

Get the names of all the constraints in the table.

Returns

array

createSql()source public

createSql( Cake\Datasource\ConnectionInterface $connection )

Generate the SQL to create the Table.

Uses the connection to access the schema dialect to generate platform specific SQL.

Parameters

Cake\Datasource\ConnectionInterface $connection
The connection to generate SQL for

Returns

array

List of SQL statements to create the table and the required indexes.


defaultValues()source public

defaultValues( )

Get a hash of columns and their default values.

Returns

array

dropConstraint()source public

dropConstraint( string $name )

Remove a constraint.

Parameters

string $name
Name of the constraint to remove

dropConstraintSql()source public

dropConstraintSql( Cake\Datasource\ConnectionInterface $connection )

Generate the SQL statements to drop the constraints to the table

Parameters

Cake\Datasource\ConnectionInterface $connection
The connection to generate SQL for.

Returns

array
SQL to drop a table.

dropSql()source public

dropSql( Cake\Datasource\ConnectionInterface $connection )

Generate the SQL to drop a table.

Uses the connection to access the schema dialect to generate platform specific SQL.

Parameters

Cake\Datasource\ConnectionInterface $connection
The connection to generate SQL for.

Returns

array
SQL to drop a table.

getOptions()source 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

array
An array of options.

hasAutoincrement()source public

hasAutoincrement( )

Check whether or not a table has an autoIncrement column defined.

Returns

boolean

index()source public

index( string $name )

Read information about an index based on name.

Parameters

string $name
The name of the index.

Returns

array|null
Array of index data, or null

indexes()source public

indexes( )

Get the names of all the indexes in the table.

Returns

array

isNullable()source 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

boolean
Whether or not the field is nullable.

isTemporary()source public

isTemporary( )

Gets whether the table is temporary in the database.

Returns

boolean
The current temporary setting.

name()source public

name( )

Get the name of the table.

Returns

string

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

array

Column name(s) for the primary key. An empty list will be returned when the table has no primary key.


setOptions()source 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


$this

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

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\Datasource\ConnectionInterface $connection )

Generate the SQL statements to truncate a table

Parameters

Cake\Datasource\ConnectionInterface $connection
The connection to generate SQL for.

Returns

array
SQL to truncate a table.

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

array

Properties detail

$_columnExtrassource

protected static array

Additional type specific properties.

[
    'string' => [
        'fixed' => null,
        'collate' => null,
    ],
    'text' => [
        'collate' => 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,
]

$_columnssource

protected array

Columns in the table.

[]

$_constraintssource

protected array

Constraints in the table.

[]

$_indexKeyssource

protected static array

The valid keys that can be used in an index definition.

[
    'type' => null,
    'columns' => [],
    'length' => [],
    'references' => [],
    'update' => 'restrict',
    'delete' => 'restrict',
]

$_indexessource

protected array

Indexes in the table.

[]

$_optionssource

protected array

Options for the table.

[]

$_tablesource

protected string

The name of the table

$_temporarysource

protected boolean

Whether or not the table is temporary

false

$_typeMapsource

protected array

A map with columns to types

[]

$_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