Schema
API reference › yii › db › mysql › Schema
Schema is the class for retrieving metadata from a MySQL database (version 8.0 and later).
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$columnSchemaClass | class-string<yii\ | Default: 'yii\. Available since 2.0.11. | Schema |
$db | yii\ | The database connection Default: null. | yii\ |
$defaultSchema | string | The default schema name used for the current session. Default: null. | yii\ |
$exceptionMap | array | Map of DB errors and corresponding exceptions If left part is found in DB error message exception class from the right part is used. Default: ['SQLSTATE[23' => 'yii\. | yii\ |
$lastInsertID read-only | string | The row ID of the last row inserted, or the last value retrieved from the sequence object. | yii\ |
$queryBuilder read-only | yii\ | The query builder for this connection. | yii\ |
$schemaChecks read-only | yii\[][] | Check constraints for all tables in the database. | yii\ |
$schemaDefaultValues read-only | yii\[] | Default value constraints for all tables in the database. | yii\ |
$schemaForeignKeys read-only | yii\[][] | Foreign keys for all tables in the database. | yii\ |
$schemaIndexes read-only | yii\[][] | Indexes for all tables in the database. | yii\ |
$schemaNames read-only | string[] | All schema names in the database, except system schemas. | yii\ |
$schemaPrimaryKeys read-only | yii\[] | Primary keys for all tables in the database. | yii\ |
$schemaUniques read-only | yii\[][] | Unique constraints for all tables in the database. | yii\ |
$serverVersion read-only | string | Server version as a string. | yii\ |
$systemSchemaNames | list<string> | List of schema names that should be excluded from getSchemaNames(). Default: []. | yii\ |
$tableNames read-only | string[] | All table names in the database. | yii\ |
$tableSchemas read-only | yii\[] | The metadata for all tables in the database. | yii\ |
$transactionIsolationLevel write-only | string | The transaction isolation level to use for this transaction. | yii\ |
$typeMap | array | Mapping from physical column types (keys) to abstract column types (values) Default: ['tinyint' => self::TYPE_TINYINT,. | Schema |
$columnQuoteCharacter protected | string| | Default: '`'. Available since 2.0.14. | Schema |
$tableQuoteCharacter protected | string| | Default: '`'. Available since 2.0.14. | Schema |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
convertException() | Converts a DB exception to a more concrete one if possible. | yii\ |
createColumnSchemaBuilder() | Create a column schema builder instance giving the type and value precision. | Schema |
createQueryBuilder() | Creates a query builder for the MySQL database. | Schema |
createSavepoint() | Creates a new savepoint. | yii\ |
findUniqueIndexes() | Returns all unique indexes for the given table. | Schema |
getLastInsertID() | Returns the ID of the last inserted row or sequence value. | yii\ |
getPdoType() | Determines the PDO type for the given PHP data value. | yii\ |
getQueryBuilder() | yii\ | |
getRawTableName() | Returns the actual name of a given table name. | yii\ |
getSchemaChecks() | Returns check constraints for all tables in the database. | yii\ |
getSchemaDefaultValues() | Returns default value constraints for all tables in the database. | yii\ |
getSchemaForeignKeys() | Returns foreign keys for all tables in the database. | yii\ |
getSchemaIndexes() | Returns indexes for all tables in the database. | yii\ |
getSchemaNames() | Returns all schema names in the database, except system schemas. | yii\ |
getSchemaPrimaryKeys() | Returns primary keys for all tables in the database. | yii\ |
getSchemaUniques() | Returns unique constraints for all tables in the database. | yii\ |
getServerVersion() | Returns a server version as a string comparable by version_compare(). | yii\ |
getTableChecks() | Obtains the check constraints information for the named table. | yii\ |
getTableDefaultValues() | Obtains the default value constraints information for the named table. | yii\ |
getTableForeignKeys() | Obtains the foreign keys information for the named table. | yii\ |
getTableIndexes() | Obtains the indexes information for the named table. | yii\ |
getTableNames() | Returns all table names in the database. | yii\ |
getTablePrimaryKey() | Obtains the primary key for the named table. | yii\ |
getTableSchema() | Obtains the metadata for the named table. | yii\ |
getTableSchemas() | Returns the metadata for all tables in the database. | yii\ |
getTableUniques() | Obtains the unique constraints information for the named table. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
insert() | Executes the INSERT command, returning primary key values. | yii\ |
isReadQuery() | Returns a value indicating whether a SQL statement is for read purpose. | yii\ |
quoteColumnName() | Quotes a column name for use in a query. | yii\ |
quoteSimpleColumnName() | Quotes a simple column name for use in a query. | yii\ |
quoteSimpleTableName() | Quotes a simple table name for use in a query. | yii\ |
quoteTableName() | Quotes a table name for use in a query. | yii\ |
quoteValue() | Quotes a string value for use in a query. | yii\ |
refresh() | Refreshes the schema. | yii\ |
refreshTableSchema() | Refreshes the particular table schema. | yii\ |
releaseSavepoint() | Releases an existing savepoint. | yii\ |
resolveRawTableName() | Resolves the table name and schema name (if any) without loading database metadata. | yii\ |
rollBackSavepoint() | Rolls back to a previously created savepoint. | yii\ |
setTransactionIsolationLevel() | Sets the isolation level of the current transaction. | yii\ |
supportsSavepoint() | yii\ | |
unquoteSimpleColumnName() | Unquotes a simple column name. | yii\ |
unquoteSimpleTableName() | Unquotes a simple table name. | yii\ |
createColumnSchema() protected | Creates a column schema for the database. | yii\ |
findColumns() protected | Collects the metadata of table columns. | Schema |
findConstraints() protected | Collects the foreign key column details for the given table. | Schema |
findSchemaNames() protected | Returns all schema names in the database, including the default one but not system schemas. | yii\ |
findTableNames() protected | Returns all table names in the database. | Schema |
getCacheKey() protected | Returns the cache key for the specified table name. | yii\ |
getCacheTag() protected | Returns the cache tag name. | yii\ |
getColumnPhpType() protected | Extracts the PHP type from abstract DB type. | yii\ |
getCreateTableSql() protected | Gets the CREATE TABLE sql string. | Schema |
getSchemaMetadata() protected | Returns the metadata of the given type for all tables in the given schema. | yii\ |
getTableMetadata() protected | Returns the metadata of the given type for the given table. | yii\ |
getTableNameParts() protected | Splits full table name into parts. | yii\ |
loadColumnSchema() protected | Loads the column information into a ColumnSchema object. | Schema |
loadTableChecks() protected | Schema | |
loadTableDefaultValues() protected | Schema | |
loadTableForeignKeys() protected | Schema | |
loadTableIndexes() protected | Schema | |
loadTablePrimaryKey() protected | Schema | |
loadTableSchema() protected | Loads the metadata for the specified table. | Schema |
loadTableUniques() protected | Schema | |
normalizePdoRowKeyCase() protected | Changes row’s array key case to lower if PDO’s one is set to uppercase. | yii\ |
resolveTableName() protected | Resolves the table name and schema name (if any). | Schema |
setTableMetadata() protected | Sets the metadata of the given type for the given table. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
TYPE_PK | 'pk' | yii\ | |
TYPE_UPK | 'upk' | yii\ | |
TYPE_BIGPK | 'bigpk' | yii\ | |
TYPE_UBIGPK | 'ubigpk' | yii\ | |
TYPE_CHAR | 'char' | yii\ | |
TYPE_STRING | 'string' | yii\ | |
TYPE_TEXT | 'text' | yii\ | |
TYPE_TINYINT | 'tinyint' | yii\ | |
TYPE_SMALLINT | 'smallint' | yii\ | |
TYPE_INTEGER | 'integer' | yii\ | |
TYPE_BIGINT | 'bigint' | yii\ | |
TYPE_FLOAT | 'float' | yii\ | |
TYPE_DOUBLE | 'double' | yii\ | |
TYPE_DECIMAL | 'decimal' | yii\ | |
TYPE_DATETIME | 'datetime' | yii\ | |
TYPE_TIMESTAMP | 'timestamp' | yii\ | |
TYPE_TIME | 'time' | yii\ | |
TYPE_DATE | 'date' | yii\ | |
TYPE_BINARY | 'binary' | yii\ | |
TYPE_BOOLEAN | 'boolean' | yii\ | |
TYPE_MONEY | 'money' | yii\ | |
TYPE_JSON | 'json' | yii\ | |
SCHEMA_CACHE_VERSION | 1 | Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes. | yii\ |
Property details
Section titled “Property details”$columnQuoteCharacter
Section titled “$columnQuoteCharacter ”protected string|string[] $columnQuoteCharacter = '`'An array of 2 characters can be used in case starting and ending characters are different.
Since
2.0.14
$tableQuoteCharacter
Section titled “$tableQuoteCharacter ”protected string|string[] $tableQuoteCharacter = '`'An array of 2 characters can be used in case starting and ending characters are different.
Since
2.0.14
Method details
Section titled “Method details”createColumnSchemaBuilder()
Section titled “createColumnSchemaBuilder() ”public function createColumnSchemaBuilder(mixed $type, mixed $length = null): yii\db\ColumnSchemaBuilderCreate a column schema builder instance giving the type and value precision.
This method may be overridden by child classes to create a DBMS-specific column schema builder.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | mixed | Type of the column. See ColumnSchemaBuilder::$type. |
$length | mixed | Length or precision of the column. See ColumnSchemaBuilder::$length. |
Returns
yii\db\ColumnSchemaBuilder: Column schema builder instance
Since
2.0.6
createQueryBuilder()
Section titled “createQueryBuilder() ”public function createQueryBuilder(): yii\db\mysql\QueryBuilderCreates a query builder for the MySQL database.
Returns
QueryBuilder: Query builder instance
findUniqueIndexes()
Section titled “findUniqueIndexes() ”public function findUniqueIndexes(yii\db\TableSchema $table): arrayReturns all unique indexes for the given table.
Each array element is of the following structure:
[ 'IndexName1' => ['col1' [, ...]], 'IndexName2' => ['col2' [, ...]],]Parameters
| Parameter | Type | Description |
|---|---|---|
$table | yii\ | The table metadata |
Returns
array: All unique indexes for the given table.
findColumns()
Section titled “findColumns() ”protected function findColumns(yii\db\TableSchema $table): boolCollects the metadata of table columns.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | yii\ | The table metadata |
Returns
bool: Whether the table exists in the database
Throws
Exception: if DB query fails
findConstraints()
Section titled “findConstraints() ”protected function findConstraints(yii\db\TableSchema $table): mixedCollects the foreign key column details for the given table.
findTableNames()
Section titled “findTableNames() ”protected function findTableNames(mixed $schema = ''): arrayReturns all table names in the database.
This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.
Parameters
| Parameter | Type | Description |
|---|---|---|
$schema | mixed | The schema of the tables. Defaults to empty string, meaning the current or default schema. |
Returns
array: All table names in the database. The names have NO schema name prefix.
Throws
yii\base\NotSupportedException: if this method is not supported by the DBMS.
getCreateTableSql()
Section titled “getCreateTableSql() ”protected function getCreateTableSql(yii\db\TableSchema $table): stringGets the CREATE TABLE sql string.
Parameters
| Parameter | Type | Description |
|---|---|---|
$table | yii\ | The table metadata |
Returns
string: $sql the result of ‘SHOW CREATE TABLE’
loadColumnSchema()
Section titled “loadColumnSchema() ”protected function loadColumnSchema(array $info): yii\db\mysql\TLoads the column information into a ColumnSchema object.
Parameters
| Parameter | Type | Description |
|---|---|---|
$info | array | Column information |
Returns
yii\db\mysql\T: The column schema object
loadTableChecks()
Section titled “loadTableChecks() ”protected function loadTableChecks(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableDefaultValues()
Section titled “loadTableDefaultValues() ”protected function loadTableDefaultValues(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
Throws
yii\base\NotSupportedException: if this method is called.
loadTableForeignKeys()
Section titled “loadTableForeignKeys() ”protected function loadTableForeignKeys(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableIndexes()
Section titled “loadTableIndexes() ”protected function loadTableIndexes(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTablePrimaryKey()
Section titled “loadTablePrimaryKey() ”protected function loadTablePrimaryKey(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
loadTableSchema()
Section titled “loadTableSchema() ”protected function loadTableSchema(mixed $name): yii\db\TableSchema|nullLoads the metadata for the specified table.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | Table name |
Returns
yii\db\TableSchema|null: DBMS-dependent table metadata, null if the table does not exist.
loadTableUniques()
Section titled “loadTableUniques() ”protected function loadTableUniques(mixed $tableName)Parameters
| Parameter | Type | Description |
|---|---|---|
$tableName | mixed |
resolveTableName()
Section titled “resolveTableName() ”protected function resolveTableName(mixed $name): yii\db\TableSchemaResolves the table name and schema name (if any).
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The table name |
Returns
yii\db\TableSchema: TableSchema with resolved table, schema, etc. names.
Throws
yii\base\NotSupportedException: if this method is not supported by the DBMS.
Since
2.0.13