CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.4 Red Velvet API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 3.4
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Auth
    • Cache
    • Collection
    • Console
    • Controller
    • Core
    • Database
      • Dialect
      • Driver
      • Exception
      • Expression
      • Log
      • Schema
      • Statement
      • Type
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
    • Validation
    • View

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.

Namespace: Cake\Database\Schema

Constants

  • string
    ACTION_CASCADE ¶
    'cascade'

    Foreign key cascade action

  • string
    ACTION_NO_ACTION ¶
    'noAction'

    Foreign key no action

  • string
    ACTION_RESTRICT ¶
    'restrict'

    Foreign key restrict action

  • string
    ACTION_SET_DEFAULT ¶
    'setDefault'

    Foreign key restrict default

  • string
    ACTION_SET_NULL ¶
    'setNull'

    Foreign key set null action

  • string
    CONSTRAINT_FOREIGN ¶
    'foreign'

    Foreign constraint type

  • string
    CONSTRAINT_PRIMARY ¶
    'primary'

    Primary constraint type

  • string
    CONSTRAINT_UNIQUE ¶
    'unique'

    Unique constraint type

  • string
    INDEX_FULLTEXT ¶
    'fulltext'

    Fulltext index type

  • string
    INDEX_INDEX ¶
    'index'

    Index - index type

  • int
    LENGTH_LONG ¶
    4294967295

    Column length when using a long column type

  • int
    LENGTH_MEDIUM ¶
    16777215

    Column length when using a medium column type

  • int
    LENGTH_TINY ¶
    255

    Column length when using a tiny column type

Property 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
    bool

    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.

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

  • getOptions() public

    Gets the options for a table.

  • hasAutoincrement() public

    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 deprecated

    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.

  • setOptions() public

    Sets the options for a table.

  • setTemporary() public

    Sets whether the table is temporary in the database.

  • temporary() public deprecated

    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): array

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() ¶ public

addColumn(string $name, array $attrs): $this

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() ¶ public

addConstraint(string $name, array $attrs): $this

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() ¶ public

addConstraintSql(Cake\Datasource\ConnectionInterface $connection): array

Generate the SQL statements to add the constraints to the table

Parameters
Cake\Datasource\ConnectionInterface $connection

The connection to generate SQL for.

Returns
array

addIndex() ¶ public

addIndex(string $name, array $attrs): $this

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() ¶ public

baseColumnType(string $column): string|null

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

column() ¶ public

column(string $name): array|null

Get column data in the table.

Parameters
string $name

The column name.

Returns
array|null

columnType() ¶ public

columnType(string $name, string|null $type = null): string|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

The type to set the column to.

Returns
string|null

columns() ¶ public

columns(): array

Get the column names in the table.

Returns
array

constraint() ¶ public

constraint(string $name): array|null

Read information about a constraint based on name.

Parameters
string $name

The name of the constraint.

Returns
array|null

constraints() ¶ public

constraints(): array

Get the names of all the constraints in the table.

Returns
array

createSql() ¶ public

createSql(Cake\Datasource\ConnectionInterface $connection): array

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

defaultValues() ¶ public

defaultValues(): array

Get a hash of columns and their default values.

Returns
array

dropConstraint() ¶ public

dropConstraint(string $name): void

Remove a constraint.

Parameters
string $name

Name of the constraint to remove

Returns
void

dropConstraintSql() ¶ public

dropConstraintSql(Cake\Datasource\ConnectionInterface $connection): array

Generate the SQL statements to drop the constraints to the table

Parameters
Cake\Datasource\ConnectionInterface $connection

The connection to generate SQL for.

Returns
array

dropSql() ¶ public

dropSql(Cake\Datasource\ConnectionInterface $connection): array

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

getOptions() ¶ public

getOptions(): array

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

hasAutoincrement() ¶ public

hasAutoincrement(): bool

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

Returns
bool

index() ¶ public

index(string $name): array|null

Read information about an index based on name.

Parameters
string $name

The name of the index.

Returns
array|null

indexes() ¶ public

indexes(): array

Get the names of all the indexes in the table.

Returns
array

isNullable() ¶ public

isNullable(string $name): bool

Check whether or not a field is nullable

Missing columns are nullable.

Parameters
string $name

The column to get the type of.

Returns
bool

isTemporary() ¶ public

isTemporary(): bool

Gets whether the table is temporary in the database.

Returns
bool

name() ¶ public

name(): string

Get the name of the table.

Returns
string

options() ¶ public

options(array|null $options = null): $this|array

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

The options to set, or null to read options.

Returns
$this|array

primaryKey() ¶ public

primaryKey(): array

Get the column(s) used for the primary key.

Returns
array

removeColumn() ¶ public

removeColumn(string $name): $this

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

setOptions() ¶ public

setOptions(array $options): $this

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() ¶ public

setTemporary(bool $temporary): $this

Sets whether the table is temporary in the database.

Parameters
bool $temporary

Whether or not the table is to be temporary.

Returns
$this

temporary() ¶ public

temporary(bool|null $temporary = null): $this|bool

Get/Set whether the table is temporary in the database

Parameters
bool|null $temporary optional

whether or not the table is to be temporary

Returns
$this|bool

truncateSql() ¶ public

truncateSql(Cake\Datasource\ConnectionInterface $connection): array

Generate the SQL statements to truncate a table

Parameters
Cake\Datasource\ConnectionInterface $connection

The connection to generate SQL for.

Returns
array

typeMap() ¶ public

typeMap(): array

Returns an array where the keys are the column names in the schema and the values the database type they have.

Returns
array

Property Detail

$_columnExtras ¶ protected static

Additional type specific properties.

Type
array

$_columnKeys ¶ protected static

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

Type
array

$_columns ¶ protected

Columns in the table.

Type
array

$_constraints ¶ protected

Constraints in the table.

Type
array

$_indexKeys ¶ protected static

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

Type
array

$_indexes ¶ protected

Indexes in the table.

Type
array

$_options ¶ protected

Options for the table.

Type
array

$_table ¶ protected

The name of the table

Type
string

$_temporary ¶ protected

Whether or not the table is temporary

Type
bool

$_typeMap ¶ protected

A map with columns to types

Type
array

$_validConstraintTypes ¶ protected static

Names of the valid constraint types.

Type
array

$_validForeignKeyActions ¶ protected static

Names of the valid foreign key actions.

Type
array

$_validIndexTypes ¶ protected static

Names of the valid index types.

Type
array

$columnLengths ¶ public static

Valid column length that can be used with text type columns

Type
array
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs