# ColumnSchema

> Represents the metadata of a column in a MySQL database table.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › [mysql](/docs/api/yii/db/mysql/index.md) › ColumnSchema

| class | `yii\db\mysql\ColumnSchema` |
| --- | --- |
| Extends | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.14.1 |
| Source | [`framework/db/mysql/ColumnSchema.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/mysql/ColumnSchema.php#L28) |

Represents the metadata of a column in a MySQL database table.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-allowNull"></span>[`$allowNull`](/docs/api/yii/db/columnschema/index.md#property-allowNull) | `bool` | Whether this column can be null. Default: `false`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-autoIncrement"></span>[`$autoIncrement`](/docs/api/yii/db/columnschema/index.md#property-autoIncrement) | `bool` | Whether this column is auto-incremental Default: `false`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-comment"></span>[`$comment`](/docs/api/yii/db/columnschema/index.md#property-comment) | `string\|null` | Comment of this column. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-dbType"></span>[`$dbType`](/docs/api/yii/db/columnschema/index.md#property-dbType) | `string` | The DB type of this column. Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-defaultValue"></span>[`$defaultValue`](/docs/api/yii/db/columnschema/index.md#property-defaultValue) | `mixed` | Default value of this column Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-enumValues"></span>[`$enumValues`](/docs/api/yii/db/columnschema/index.md#property-enumValues) | `array\|null` | Enumerable values. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-isDefaultExpression"></span>`$isDefaultExpression` | `bool` | Whether MySQL reports the column default as an expression (`DEFAULT_GENERATED`). Default: `false`. Available since 22.0. | `ColumnSchema` |
| <span id="property-isPrimaryKey"></span>[`$isPrimaryKey`](/docs/api/yii/db/columnschema/index.md#property-isPrimaryKey) | `bool\|null` | Whether this column is a primary key Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-name"></span>[`$name`](/docs/api/yii/db/columnschema/index.md#property-name) | `string` | Name of this column (without quotes). Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-phpType"></span>[`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) | `string` | The PHP type of this column. Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-precision"></span>[`$precision`](/docs/api/yii/db/columnschema/index.md#property-precision) | `int\|null` | Precision of the column data, if it is numeric. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-scale"></span>[`$scale`](/docs/api/yii/db/columnschema/index.md#property-scale) | `int\|null` | Scale of the column data, if it is numeric. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-size"></span>[`$size`](/docs/api/yii/db/columnschema/index.md#property-size) | `int\|null` | Display size of the column. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-type"></span>[`$type`](/docs/api/yii/db/columnschema/index.md#property-type) | `string` | Abstract type of this column. Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-unsigned"></span>[`$unsigned`](/docs/api/yii/db/columnschema/index.md#property-unsigned) | `bool` | Whether this column is unsigned. Default: `false`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`dbTypecast()`](#method-dbTypecast) | Converts the input value according to [`$type`](/docs/api/yii/db/columnschema/index.md#property-type) and [`$dbType`](/docs/api/yii/db/columnschema/index.md#property-dbType) for use in a db query. | `ColumnSchema` |
| [`defaultPhpTypecast()`](#method-defaultPhpTypecast) | Converts a MySQL column default value to its PHP representation. | `ColumnSchema` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-isType"></span>[`isType()`](/docs/api/yii/db/columnschema/index.md#method-isType) | Returns whether this column uses the given abstract type. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| [`phpTypecast()`](#method-phpTypecast) | Converts the input value according to [`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) after retrieval from the database. | `ColumnSchema` |
| <span id="method-typecast"></span>[`typecast()`](/docs/api/yii/db/columnschema/index.md#method-typecast) protected | Converts the input value according to [`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) after retrieval from the database. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |

## Method details

### dbTypecast() <span id="method-dbTypecast"></span>

```php
public function dbTypecast(mixed $value): mixed
```

Converts the input value according to [`$type`](/docs/api/yii/db/columnschema/index.md#property-type) and [`$dbType`](/docs/api/yii/db/columnschema/index.md#property-dbType) for use in a db query.

If the value is null or an [`Expression`](/docs/api/yii/db/expression/index.md), it will not be converted.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Input value |

**Returns**

`mixed`: Converted value. This may also be an array containing the value as the first element and the PDO type as the second element.

### defaultPhpTypecast() <span id="method-defaultPhpTypecast"></span>

```php
public function defaultPhpTypecast(mixed $value): mixed
```

Converts a MySQL column default value to its PHP representation.

Handles MySQL-specific default value formats:
- `null` to `null`.
- `CURRENT_TIMESTAMP` / `current_timestamp()` on temporal columns (`timestamp`, `datetime`, `date`, `time`) to an
  [`Expression`](/docs/api/yii/db/expression/index.md), preserving any declared fractional-seconds precision such as `CURRENT_TIMESTAMP(3)`.
- quoted string literals on `text` and `blob` columns, and on any column flagged through
  [`$isDefaultExpression`](/docs/api/yii/db/mysql/columnschema/index.md#property-isDefaultExpression), to their PHP value: the character-set introducer MySQL prepends is discarded and
  the escape sequences in `\yii\db\mysql\DEFAULT_LITERAL_ESCAPES` are resolved. `json` columns are exempt.
- remaining expression defaults flagged through [`$isDefaultExpression`](/docs/api/yii/db/mysql/columnschema/index.md#property-isDefaultExpression) to an [`Expression`](/docs/api/yii/db/expression/index.md).
- `json` defaults without the flag to their decoded value when the string is valid JSON, or to an
  [`Expression`](/docs/api/yii/db/expression/index.md) otherwise; MariaDB reports expression-form defaults without metadata.
- remaining `text` defaults without the flag to an [`Expression`](/docs/api/yii/db/expression/index.md), preserving MariaDB's expression-form SQL.
- bit defaults (`b'...'`) when `$dbType` starts with `bit` to their integer value via `bindec()`.
- everything else delegates to [`phpTypecast()`](/docs/api/yii/db/mysql/columnschema/index.md#method-phpTypecast).

Branch order is significant: MySQL also flags plain `CURRENT_TIMESTAMP` defaults as `DEFAULT_GENERATED`, so the
normalization above must win over the generic expression wrapping. Flagged defaults carry an extra metadata
escape layer (`\'`, `\\`) that is removed before literal parsing; MariaDB reports the expression verbatim.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Default value in the format reported by `SHOW FULL COLUMNS`. |

**Returns**

`mixed`: Converted value.

**Since**

22.0

### phpTypecast() <span id="method-phpTypecast"></span>

```php
public function phpTypecast(mixed $value): mixed
```

Converts the input value according to [`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) after retrieval from the database.

If the value is null or an [`Expression`](/docs/api/yii/db/expression/index.md), it will not be converted.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Input value |

**Returns**

`mixed`: Converted value