| public static primaryKey ( boolean $primaryKey = true ) | ||
| $primaryKey | boolean | |
public function primaryKey(bool $primaryKey = true): static
{
$this->primaryKey = $primaryKey;
return $this;
}
| Inheritance | Yiisoft\Db\Schema\Column\DateTimeColumn » Yiisoft\Db\Schema\Column\AbstractColumn |
|---|---|
| Implements | Yiisoft\Db\Schema\Column\ColumnInterface |
Represents the metadata for a datetime column.
Supported abstract column types:
ColumnType::TIMESTAMPColumnType::DATETIMEColumnType::DATETIMETZColumnType::TIMEColumnType::TIMETZColumnType::DATEColumnType::INTEGERColumnType::BIGINTColumnType::FLOATColumnType::DOUBLEColumnType::DECIMALPossible issues:
TIMESTAMP column type values from database session time zone to UTC for storage,
and back from UTC to the session time zone when retrieve the values;TIMESTAMP WITH LOCAL TIME ZONE column type values from database session time zone to
the database time zone for storage, and back from the database time zone to the session time zone when retrieve
the values.Both of them do not store time zone offset and require to convert datetime values to the database session time zone before insert and back to the PHP time zone after retrieve the values. This will be done in the {@see \Yiisoft\Db\Schema\Column\dbTypecast()} and {@see \Yiisoft\Db\Schema\Column\phpTypecast()} methods and guarantees that the values are stored in the database in the correct time zone.
To avoid possible time zone issues with the datetime values conversion, it is recommended to set the PHP and database time zones to UTC.
| Property | Type | Description | Defined By |
|---|---|---|---|
| $dbTimezone | string | The database time zone to be used when converting datetime values before inserting them into the database
and when converting them back to PHP DateTimeImmutable objects. It is used when the column type does not have
time zone information. Use empty string to disable time zone conversion. |
Yiisoft\Db\Schema\Column\DateTimeColumn |
| $format | ?string | Yiisoft\Db\Schema\Column\DateTimeColumn | |
| $phpTimezone | string | The PHP time zone for the string datetime values when converting them to DateTimeImmutable objects before
inserting them into the database or after retrieving them from the database. Use empty string to use current PHP
time zone. |
Yiisoft\Db\Schema\Column\DateTimeColumn |
| $shouldConvertTimezone | ?bool | Yiisoft\Db\Schema\Column\DateTimeColumn |
| Method | Description | Defined By |
|---|---|---|
| getFormat() | Yiisoft\Db\Schema\Column\DateTimeColumn | |
| getMillisecondsFormat() | Yiisoft\Db\Schema\Column\DateTimeColumn | |
| getPhpTimezone() | Returns the PHP time zone for the string datetime values when converting them to DateTimeImmutable objects
before inserting them into the database or after retrieving them from the database. |
Yiisoft\Db\Schema\Column\DateTimeColumn |
| shouldConvertTimezone() | Yiisoft\Db\Schema\Column\DateTimeColumn | |
| throwWrongTypeException() | Yiisoft\Db\Schema\Column\AbstractColumn |
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_TYPE | \Yiisoft\Db\Constant\ColumnType::DATETIME | Yiisoft\Db\Schema\Column\DateTimeColumn |
| public mixed __construct ( string|null $type = null, boolean $autoIncrement = false, string|null $check = null, string|null $comment = null, boolean $computed = false, string|null $dbType = null, string|null $extra = null, boolean $primaryKey = false, string|null $name = null, boolean|null $notNull = null, Yiisoft\Db\Constraint\ForeignKey|null $reference = null, integer|null $scale = null, integer|null $size = null, boolean $unique = false, boolean $unsigned = false, mixed $args ) | ||
| $type | string|null |
The column's abstract type. |
| $autoIncrement | boolean |
Whether the column is auto-incremental. |
| $check | string|null |
The check constraint for the column. |
| $comment | string|null |
The column's comment. |
| $computed | boolean |
Whether the column is a computed column. |
| $dbType | string|null |
The column's database type. |
| $extra | string|null |
Any extra information that needs to be appended to the column's definition. |
| $primaryKey | boolean |
Whether the column is a primary key. |
| $name | string|null |
The column's name. |
| $notNull | boolean|null |
Whether the column is not nullable. |
| $reference | Yiisoft\Db\Constraint\ForeignKey|null |
The foreign key constraint. |
| $scale | integer|null |
The number of digits to the right of the decimal point. |
| $size | integer|null |
The column's size. |
| $unique | boolean |
Whether the column is unique. |
| $unsigned | boolean |
Whether the column is unsigned. |
| $args | mixed |
Additional arguments to be passed to the constructor. |
public function __construct(
?string $type = null,
private bool $autoIncrement = false,
private ?string $check = null,
private ?string $comment = null,
private bool $computed = false,
private ?string $dbType = null,
private ?string $extra = null,
private bool $primaryKey = false,
private ?string $name = null,
private ?bool $notNull = null,
private ?ForeignKey $reference = null,
private ?int $scale = null,
private ?int $size = null,
private bool $unique = false,
private bool $unsigned = false,
mixed ...$args,
) {
$this->type = $type ?? static::DEFAULT_TYPE;
if (array_key_exists('defaultValue', $args)) {
$this->defaultValue = $args['defaultValue'];
unset($args['defaultValue']);
}
/** @psalm-var array<string, mixed> $args */
foreach ($args as $property => $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
}
}
| public static autoIncrement ( boolean $autoIncrement = true ) | ||
| $autoIncrement | boolean | |
public function autoIncrement(bool $autoIncrement = true): static
{
$this->autoIncrement = $autoIncrement;
return $this;
}
Defined in: Yiisoft\Db\Schema\Column\AbstractColumn::check()
| public static check ( ?string $check ) | ||
| $check | ?string | |
public function check(?string $check): static
{
$this->check = $check;
return $this;
}
| public static comment ( ?string $comment ) | ||
| $comment | ?string | |
public function comment(?string $comment): static
{
$this->comment = $comment;
return $this;
}
| public static computed ( boolean $computed = true ) | ||
| $computed | boolean | |
public function computed(bool $computed = true): static
{
$this->computed = $computed;
return $this;
}
| public static dbType ( ?string $dbType ) | ||
| $dbType | ?string | |
public function dbType(?string $dbType): static
{
$this->dbType = $dbType;
return $this;
}
| public float|int|string|\Yiisoft\Db\Expression\ExpressionInterface|null dbTypecast ( mixed $value ) | ||
| $value | mixed |
The value representing datetime or time to be typecasted to the database format. Possible values:
If the value is |
public function dbTypecast(mixed $value): float|int|string|ExpressionInterface|null
{
/** @psalm-suppress MixedArgument, PossiblyFalseArgument */
return match (gettype($value)) {
GettypeResult::NULL => null,
GettypeResult::STRING => $this->dbTypecastString($value),
GettypeResult::INTEGER => $this->dbTypecastDateTime(DateTimeImmutable::createFromFormat('U', (string) $value)),
GettypeResult::DOUBLE => $this->dbTypecastDateTime(DateTimeImmutable::createFromFormat('U.u', (string) $value)),
GettypeResult::OBJECT => match (true) {
$value instanceof DateTimeImmutable => $this->dbTypecastDateTime($value),
$value instanceof DateTimeInterface => $this->dbTypecastDateTime(DateTimeImmutable::createFromInterface($value)),
$value instanceof ExpressionInterface => $value,
$value instanceof Stringable => $this->dbTypecastString((string) $value),
default => $this->throwWrongTypeException($value::class),
},
default => $this->throwWrongTypeException(gettype($value)),
};
}
| public static defaultValue ( mixed $defaultValue ) | ||
| $defaultValue | mixed | |
public function defaultValue(mixed $defaultValue): static
{
$this->defaultValue = $defaultValue;
return $this;
}
Defined in: Yiisoft\Db\Schema\Column\AbstractColumn::extra()
| public static extra ( ?string $extra ) | ||
| $extra | ?string | |
public function extra(?string $extra): static
{
$this->extra = $extra;
return $this;
}
| public ?string getCheck ( ) |
public function getCheck(): ?string
{
return $this->check;
}
| public ?string getComment ( ) |
public function getComment(): ?string
{
return $this->comment;
}
| public ?string getDbType ( ) |
public function getDbType(): ?string
{
return $this->dbType;
}
| public mixed getDefaultValue ( ) |
public function getDefaultValue(): mixed
{
return $this->defaultValue ?? null;
}
| public ?string getExtra ( ) |
public function getExtra(): ?string
{
return $this->extra;
}
| protected string getFormat ( ) |
protected function getFormat(): string
{
return $this->format ??= match ($this->getType()) {
ColumnType::TIMESTAMP,
ColumnType::DATETIME => 'Y-m-d H:i:s' . $this->getMillisecondsFormat(),
ColumnType::DATETIMETZ => 'Y-m-d H:i:s' . $this->getMillisecondsFormat() . 'P',
ColumnType::TIME => 'H:i:s' . $this->getMillisecondsFormat(),
ColumnType::TIMETZ => 'H:i:s' . $this->getMillisecondsFormat() . 'P',
ColumnType::DATE => 'Y-m-d',
ColumnType::INTEGER,
ColumnType::BIGINT => 'U',
ColumnType::FLOAT ,
ColumnType::DOUBLE,
ColumnType::DECIMAL => 'U.u',
default => throw new UnexpectedValueException(
'Unsupported abstract column type ' . $this->getType() . ' for ' . static::class . ' class.',
),
};
}
| protected string getMillisecondsFormat ( ) |
protected function getMillisecondsFormat(): string
{
return match ($this->getSize()) {
0 => '',
1, 2, 3 => '.v',
default => '.u',
};
}
| public ?string getName ( ) |
public function getName(): ?string
{
return $this->name;
}
Returns the PHP time zone for the string datetime values when converting them to DateTimeImmutable objects
before inserting them into the database or after retrieving them from the database.
| protected string getPhpTimezone ( ) |
protected function getPhpTimezone(): string
{
return empty($this->phpTimezone)
? date_default_timezone_get()
: $this->phpTimezone;
}
| public ?\Yiisoft\Db\Constraint\ForeignKey getReference ( ) |
public function getReference(): ?ForeignKey
{
return $this->reference;
}
| public ?int getScale ( ) |
public function getScale(): ?int
{
return $this->scale;
}
| public ?int getSize ( ) |
public function getSize(): ?int
{
return $this->size;
}
| public boolean hasDefaultValue ( ) |
public function hasDefaultValue(): bool
{
return property_exists($this, 'defaultValue');
}
| public boolean isAutoIncrement ( ) |
public function isAutoIncrement(): bool
{
return $this->autoIncrement;
}
| public boolean isComputed ( ) |
public function isComputed(): bool
{
return $this->computed;
}
| public ?bool isNotNull ( ) |
public function isNotNull(): ?bool
{
return $this->notNull;
}
| public boolean isPrimaryKey ( ) |
public function isPrimaryKey(): bool
{
return $this->primaryKey;
}
| public boolean isUnsigned ( ) |
public function isUnsigned(): bool
{
return $this->unsigned;
}
| public static notNull ( ?bool $notNull = true ) | ||
| $notNull | ?bool | |
public function notNull(?bool $notNull = true): static
{
$this->notNull = $notNull;
return $this;
}
Defined in: Yiisoft\Db\Schema\Column\AbstractColumn::null()
| public static null ( ) |
public function null(): static
{
$this->notNull = false;
return $this;
}
Converts the value from the database format to PHP DateTimeImmutable object.
If the database type does not have time zone information, the time zone will be set to the current PHP time zone.
| public ?\DateTimeImmutable phpTypecast ( string|null $value ) | ||
| $value | string|null | |
public function phpTypecast(mixed $value): ?DateTimeImmutable
{
if (is_string($value)) {
$phpTimezone = $this->getPhpTimezone();
if ($this->shouldConvertTimezone()) {
/** @psalm-suppress ArgumentTypeCoercion */
$datetime = new DateTimeImmutable($value, new DateTimeZone($this->dbTimezone));
if ($phpTimezone !== $this->dbTimezone) {
return $datetime->setTimezone(new DateTimeZone($phpTimezone));
}
return $datetime;
}
return new DateTimeImmutable($value, new DateTimeZone($phpTimezone));
}
return $value;
}
| public static primaryKey ( boolean $primaryKey = true ) | ||
| $primaryKey | boolean | |
public function primaryKey(bool $primaryKey = true): static
{
$this->primaryKey = $primaryKey;
return $this;
}
| public static reference ( ?\Yiisoft\Db\Constraint\ForeignKey $reference ) | ||
| $reference | ?\Yiisoft\Db\Constraint\ForeignKey | |
public function reference(?ForeignKey $reference): static
{
$this->reference = $reference;
return $this;
}
Defined in: Yiisoft\Db\Schema\Column\AbstractColumn::scale()
| public static scale ( ?int $scale ) | ||
| $scale | ?int | |
public function scale(?int $scale): static
{
$this->scale = $scale;
return $this;
}
| protected boolean shouldConvertTimezone ( ) |
protected function shouldConvertTimezone(): bool
{
return $this->shouldConvertTimezone ??= $this->dbTimezone !== '' && match ($this->getType()) {
ColumnType::DATETIMETZ,
ColumnType::TIMETZ,
ColumnType::DATE => false,
default => true,
};
}
Defined in: Yiisoft\Db\Schema\Column\AbstractColumn::size()
| public static size ( ?int $size ) | ||
| $size | ?int | |
public function size(?int $size): static
{
$this->size = $size;
return $this;
}
| protected never throwWrongTypeException ( string $type ) | ||
| $type | string | |
protected function throwWrongTypeException(string $type): never
{
throw new InvalidArgumentException("Wrong $type value for $this->type column.");
}
Defined in: Yiisoft\Db\Schema\Column\AbstractColumn::type()
| public static type ( string $type ) | ||
| $type | string | |
public function type(string $type): static
{
$this->type = $type;
return $this;
}
| public static unique ( boolean $unique = true ) | ||
| $unique | boolean | |
public function unique(bool $unique = true): static
{
$this->unique = $unique;
return $this;
}
| public static unsigned ( boolean $unsigned = true ) | ||
| $unsigned | boolean | |
public function unsigned(bool $unsigned = true): static
{
$this->unsigned = $unsigned;
return $this;
}
| public static withName ( ?string $name ) | ||
| $name | ?string | |
public function withName(?string $name): static
{
$new = clone $this;
$new->name = $name;
return $new;
}
Signup or Login in order to comment.