cakephp / 3.1.13 / class-cake.database.schema.table.html /

Class Table

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 Table instances can be added to Schema\Collection objects. They can also be converted into SQL using the createSql(), dropSql() and truncateSql() methods.

Namespace: Cake\ Database\ Schema
Located at Database/Schema/Table.php

Method Detail

__constructsource public

__construct( string $table , array $columns [] )

Constructor.

Parameters

string $table
The table name.
array $columns optional []
The list of columns for the schema.

_checkForeignKeysource protected

_checkForeignKey( array $attrs )

Helper method to check/validate foreign keys.

Parameters

array $attrs
Attributes to set.

Returns

array
array

Throws

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

addColumnsource 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

mixed
$this

addConstraintsource 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

mixed
$this

Throws

Cake\Database\Exception
\Cake\Database\Exception

addConstraintSqlsource 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.

addIndexsource 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

mixed
$this

Throws

Cake\Database\Exception
\Cake\Database\Exception

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

columnsource public

column( string $name )

Get column data in the table.

Parameters

string $name
The column name.

Returns

array|null
Column data or null.

columnTypesource public

columnType( string $name , string $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 $type optional null
The type to set the column to.

Returns

string|null
Either the column type or null.

columnssource public

columns( )

Get the column names in the table.

Returns

array
array

constraintsource 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

constraintssource public

constraints( )

Get the names of all the constraints in the table.

Returns

array
array

createSqlsource 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.

defaultValuessource public

defaultValues( )

Get a hash of columns and their default values.

Returns

array
array

dropConstraintsource public

dropConstraint( string $name )

Remove a constraint.

Parameters

string $name
Name of the constraint to remove

dropConstraintSqlsource 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.

dropSqlsource 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.

hasAutoincrementsource public

hasAutoincrement( )

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

Returns

boolean
bool

indexsource 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

indexessource public

indexes( )

Get the names of all the indexes in the table.

Returns

array
array

isNullablesource 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.

namesource public

name( )

Get the name of the table.

Returns

string
string

optionssource 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.

Parameters

array|null $options optional null
The options to set, or null to read options.

Returns

mixed
$this|array Either the table instance, or an array of options when reading.

primaryKeysource 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.

temporarysource public

temporary( boolean|null $set null )

Get/Set whether the table is temporary in the database

Parameters

boolean|null $set optional null
whether or not the table is to be temporary

Returns

mixed
$this|bool Either the table instance, the current temporary setting

truncateSqlsource 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.

typeMapsource 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
array

Constants summary

string ACTION_CASCADE

Foreign key cascade action

'cascade'
string ACTION_NO_ACTION

Foreign key no action

'noAction'
string ACTION_RESTRICT

Foreign key restrict action

'restrict'
string ACTION_SET_DEFAULT

Foreign key restrict default

'setDefault'
string ACTION_SET_NULL

Foreign key set null action

'setNull'
string CONSTRAINT_FOREIGN

Foreign constraint type

'foreign'
string CONSTRAINT_PRIMARY

Primary constraint type

'primary'
string CONSTRAINT_UNIQUE

Unique constraint type

'unique'
string INDEX_FULLTEXT

Fulltext index type

'fulltext'
string INDEX_INDEX

Index - index type

'index'

Properties summary

$_columnExtrassource

protected static array

Additional type specific properties.

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

© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.Database.Schema.Table.html