ColumnSchemaBuilder
API reference › yii › db › ColumnSchemaBuilder
4 subclasses
ColumnSchemaBuilder helps to define database schema types using a PHP interface.
See SchemaBuilderTrait for more detailed description and usage examples.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$categoryMap | array | Mapping of abstract column types (keys) to type categories (values). | ColumnSchemaBuilder |
$comment | string | Comment value of the column. Default: null. Available since 2.0.8. | ColumnSchemaBuilder |
$db | Connection | The current database connection. Default: null. Available since 2.0.8. | ColumnSchemaBuilder |
$typeCategoryMap static | array | Mapping of abstract column types (keys) to type categories (values). Default: [\. Available since 2.0.43. | ColumnSchemaBuilder |
$typeDefinition read-only | string | Abstract type plus optional length, for example string(255). Available since 22.0. | ColumnSchemaBuilder |
$after protected | string | The column after which this column will be added. Default: null. Available since 2.0.8. | ColumnSchemaBuilder |
$append protected | mixed | SQL string to be appended to column schema definition. Default: null. Available since 2.0.9. | ColumnSchemaBuilder |
$check protected | string | The CHECK constraint for the column. Default: null. | ColumnSchemaBuilder |
$default protected | mixed | Default value of the column. Default: null. | ColumnSchemaBuilder |
$isFirst protected | bool | Whether this column is to be inserted at the beginning of the table. Default: null. Available since 2.0.8. | ColumnSchemaBuilder |
$isNotNull protected | bool| | Whether the column is or not nullable. Default: null. | ColumnSchemaBuilder |
$isUnique protected | bool | Whether the column values should be unique. Default: false. | ColumnSchemaBuilder |
$isUnsigned protected | bool | Whether the column values should be unsigned. Default: false. Available since 2.0.7. | ColumnSchemaBuilder |
$length protected | int| | Column size or precision definition. Default: null. | ColumnSchemaBuilder |
$type protected | string | The column type definition such as INTEGER, VARCHAR, DATETIME, etc. Default: null. | ColumnSchemaBuilder |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Create a column schema builder instance giving the type and value precision. | ColumnSchemaBuilder |
__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\ |
__toString() | Builds the full string for the column’s schema. | ColumnSchemaBuilder |
__unset() | Sets an object property to null. | yii\ |
after() | Adds an AFTER constraint to the column. | ColumnSchemaBuilder |
append() | Specify additional SQL to be appended to column definition. | ColumnSchemaBuilder |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
check() | Sets a CHECK constraint for the column. | ColumnSchemaBuilder |
className() static | Returns the fully qualified name of this class. | yii\ |
comment() | Specifies the comment for column. | ColumnSchemaBuilder |
defaultExpression() | Specify the default SQL expression for the column. | ColumnSchemaBuilder |
defaultValue() | Specify the default value for the column. | ColumnSchemaBuilder |
first() | Adds an FIRST constraint to the column. | ColumnSchemaBuilder |
getAppend() | Returns the raw SQL fragment appended to the column definition. | ColumnSchemaBuilder |
getCategoryMap() | ColumnSchemaBuilder | |
getCheck() | Returns the raw CHECK constraint expression of the column. | ColumnSchemaBuilder |
getDefault() | Returns the raw default value of the column. | ColumnSchemaBuilder |
getTypeDefinition() | Returns the abstract type together with its length or precision, without any constraint keywords. | ColumnSchemaBuilder |
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\ |
isNotNull() | Returns the nullability state of the column as a tri-state value. | ColumnSchemaBuilder |
isUnique() | Returns whether the column carries a UNIQUE constraint. | ColumnSchemaBuilder |
notNull() | Adds a NOT NULL constraint to the column. | ColumnSchemaBuilder |
null() | Adds a NULL constraint to the column. | ColumnSchemaBuilder |
setCategoryMap() | ColumnSchemaBuilder | |
unique() | Adds a UNIQUE constraint to the column. | ColumnSchemaBuilder |
unsigned() | Marks column as unsigned. | ColumnSchemaBuilder |
buildAfterString() protected | Builds the after constraint for the column. Defaults to unsupported. | ColumnSchemaBuilder |
buildAppendString() protected | Builds the custom string that’s appended to column definition. | ColumnSchemaBuilder |
buildCheckString() protected | Builds the check constraint for the column. | ColumnSchemaBuilder |
buildCommentString() protected | Builds the comment specification for the column. | ColumnSchemaBuilder |
buildCompleteString() protected | Returns the complete column definition from input format. | ColumnSchemaBuilder |
buildDefaultString() protected | Builds the default value specification for the column. | ColumnSchemaBuilder |
buildDefaultValue() protected | Return the default value for the column. | ColumnSchemaBuilder |
buildFirstString() protected | Builds the first constraint for the column. Defaults to unsupported. | ColumnSchemaBuilder |
buildLengthString() protected | Builds the length/precision part of the column. | ColumnSchemaBuilder |
buildNotNullString() protected | Builds the not null constraint for the column. | ColumnSchemaBuilder |
buildUniqueString() protected | Builds the unique constraint for the column. | ColumnSchemaBuilder |
buildUnsignedString() protected | Builds the unsigned string for column. Defaults to unsupported. | ColumnSchemaBuilder |
getTypeCategory() protected | Returns the category of the column type. | ColumnSchemaBuilder |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
CATEGORY_PK | 'pk' | ColumnSchemaBuilder | |
CATEGORY_STRING | 'string' | ColumnSchemaBuilder | |
CATEGORY_NUMERIC | 'numeric' | ColumnSchemaBuilder | |
CATEGORY_TIME | 'time' | ColumnSchemaBuilder | |
CATEGORY_OTHER | 'other' | ColumnSchemaBuilder |
Property details
Section titled “Property details”public yii\db\Connection $db = nullThe current database connection.
It is used mainly to escape strings safely when building the final column schema string.
Since
2.0.8
$isNotNull
Section titled “$isNotNull ”protected bool|null $isNotNull = nullWhether the column is or not nullable.
If this is true, a NOT NULL constraint will be added.
If this is false, a NULL constraint will be added.
$isUnique
Section titled “$isUnique ”protected bool $isUnique = falseWhether the column values should be unique.
If this is true, a UNIQUE constraint will be added.
$isUnsigned
Section titled “$isUnsigned ”protected bool $isUnsigned = falseWhether the column values should be unsigned.
If this is true, an UNSIGNED keyword will be added.
Since
2.0.7
$length
Section titled “$length ”protected int|string|array $length = nullColumn size or precision definition.
This is what goes into the parenthesis after the column type. This can be either a string, an integer or an array. If it is an array, the array values will be joined into a string separated by comma.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(string $type, int|string|array|null $length = null, yii\db\Connection|null $db = null, array $config = [])Create a column schema builder instance giving the type and value precision.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | string | Type of the column. See $type. |
$length | int| | Length or precision of the column. See $length. |
$db | Connection| | The current database connection. See $db. |
$config | array | Name-value pairs that will be used to initialize the object properties |
__toString()
Section titled “__toString() ”public function __toString(): stringBuilds the full string for the column’s schema.
Returns
string
after()
Section titled “after() ”public function after(string $after): $thisAdds an AFTER constraint to the column.
Note: MySQL and Oracle support only.
Parameters
| Parameter | Type | Description |
|---|---|---|
$after | string | The column after which $this column will be added. |
Returns
$this
Since
2.0.8
append()
Section titled “append() ”public function append(string $sql): $thisSpecify additional SQL to be appended to column definition.
Position modifiers will be appended after column definition in databases that support them.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sql | string | The SQL string to be appended. |
Returns
$this
Since
2.0.9
check()
Section titled “check() ”public function check(string $check): $thisSets a CHECK constraint for the column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$check | string | The SQL of the CHECK constraint to be added. |
Returns
$this
comment()
Section titled “comment() ”public function comment(string $comment): $thisSpecifies the comment for column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$comment | string | The comment |
Returns
$this
Since
2.0.8
defaultExpression()
Section titled “defaultExpression() ”public function defaultExpression(string $default): $thisSpecify the default SQL expression for the column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$default | string | The default value expression. |
Returns
$this
Since
2.0.7
defaultValue()
Section titled “defaultValue() ”public function defaultValue(mixed $default): $thisSpecify the default value for the column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$default | mixed | The default value. |
Returns
$this
first()
Section titled “first() ”public function first(): $thisAdds an FIRST constraint to the column.
Note: MySQL and Oracle support only.
Returns
$this
Since
2.0.8
getAppend()
Section titled “getAppend() ”public function getAppend(): string|nullReturns the raw SQL fragment appended to the column definition.
Returns
string|null: Appended SQL fragment, or null when none is set.
Since
22.0
getCategoryMap()
Section titled “getCategoryMap() ”public function getCategoryMap(): arrayReturns
array: Mapping of abstract column types (keys) to type categories (values).
Since
2.0.43
getCheck()
Section titled “getCheck() ”public function getCheck(): string|nullReturns the raw CHECK constraint expression of the column.
Returns
string|null: CHECK expression, or null when no constraint is set.
Since
22.0
getDefault()
Section titled “getDefault() ”public function getDefault(): mixedReturns the raw default value of the column.
Returns
mixed: Scalar value, an Expression when defaultExpression() was used, or null when unset.
Since
22.0
getTypeDefinition()
Section titled “getTypeDefinition() ”public function getTypeDefinition(): stringReturns the abstract type together with its length or precision, without any constraint keywords.
The result is intended for QueryBuilder::getColumnType() to resolve the physical column type.
Returns
string: Abstract type plus optional length, for example string(255).
Since
22.0
isNotNull()
Section titled “isNotNull() ”public function isNotNull(): bool|nullReturns the nullability state of the column as a tri-state value.
Returns
bool|null: true for NOT NULL, false for NULL, or null when the nullability is unspecified.
Since
22.0
isUnique()
Section titled “isUnique() ”public function isUnique(): boolReturns whether the column carries a UNIQUE constraint.
Returns
bool: true when a UNIQUE constraint is set; false otherwise.
Since
22.0
notNull()
Section titled “notNull() ”public function notNull(): $thisAdds a NOT NULL constraint to the column.
Returns
$this
null()
Section titled “null() ”public function null(): $thisAdds a NULL constraint to the column.
Returns
$this
Since
2.0.9
setCategoryMap()
Section titled “setCategoryMap() ”public function setCategoryMap(array $categoryMap): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$categoryMap | array | Mapping of abstract column types (keys) to type categories (values). |
Returns
mixed
Since
2.0.43
unique()
Section titled “unique() ”public function unique(): $thisAdds a UNIQUE constraint to the column.
Returns
$this
unsigned()
Section titled “unsigned() ”public function unsigned(): $thisMarks column as unsigned.
Returns
$this
Since
2.0.7
buildAfterString()
Section titled “buildAfterString() ”protected function buildAfterString(): stringBuilds the after constraint for the column. Defaults to unsupported.
Returns
string: A string containing the AFTER constraint.
Since
2.0.8
buildAppendString()
Section titled “buildAppendString() ”protected function buildAppendString(): stringBuilds the custom string that’s appended to column definition.
Returns
string: Custom string to append.
Since
2.0.9
buildCheckString()
Section titled “buildCheckString() ”protected function buildCheckString(): stringBuilds the check constraint for the column.
Returns
string: A string containing the CHECK constraint.
buildCommentString()
Section titled “buildCommentString() ”protected function buildCommentString(): stringBuilds the comment specification for the column.
Returns
string: A string containing the COMMENT keyword and the comment itself
Since
2.0.8
buildCompleteString()
Section titled “buildCompleteString() ”protected function buildCompleteString(string $format): stringReturns the complete column definition from input format.
Parameters
| Parameter | Type | Description |
|---|---|---|
$format | string | The format of the definition. |
Returns
string: A string containing the complete column definition.
Since
2.0.8
buildDefaultString()
Section titled “buildDefaultString() ”protected function buildDefaultString(): stringBuilds the default value specification for the column.
Returns
string: String with default value of column.
buildDefaultValue()
Section titled “buildDefaultValue() ”protected function buildDefaultValue(): string|nullReturn the default value for the column.
Returns
string|null: String with default value of column.
buildFirstString()
Section titled “buildFirstString() ”protected function buildFirstString(): stringBuilds the first constraint for the column. Defaults to unsupported.
Returns
string: A string containing the FIRST constraint.
Since
2.0.8
buildLengthString()
Section titled “buildLengthString() ”protected function buildLengthString(): stringBuilds the length/precision part of the column.
Returns
string
buildNotNullString()
Section titled “buildNotNullString() ”protected function buildNotNullString(): stringBuilds the not null constraint for the column.
Returns
string: Returns ‘NOT NULL’ if isNotNull() is true, ‘NULL’ if isNotNull() is false or an empty string otherwise.
buildUniqueString()
Section titled “buildUniqueString() ”protected function buildUniqueString(): stringBuilds the unique constraint for the column.
Returns
string: Returns string ‘UNIQUE’ if isUnique() is true, otherwise it returns an empty string.
buildUnsignedString()
Section titled “buildUnsignedString() ”protected function buildUnsignedString(): stringBuilds the unsigned string for column. Defaults to unsupported.
Returns
string: A string containing UNSIGNED keyword.
Since
2.0.7
getTypeCategory()
Section titled “getTypeCategory() ”protected function getTypeCategory(): stringReturns the category of the column type.
Returns
string: A string containing the column type category name.
Since
2.0.8