diff --git a/src/Phinx/Db/Adapter/AdapterInterface.php b/src/Phinx/Db/Adapter/AdapterInterface.php index ecce738af..dbaf78d9d 100644 --- a/src/Phinx/Db/Adapter/AdapterInterface.php +++ b/src/Phinx/Db/Adapter/AdapterInterface.php @@ -535,4 +535,10 @@ public function dropSchema(string $schemaName): void; * @return mixed */ public function castToBool(mixed $value): mixed; + + /** + * @param string|null $value Date & time, in iso-format (Y-m-d H:i:s) + * @return string + */ + public function castToDate(?string $value); } diff --git a/src/Phinx/Db/Adapter/AdapterWrapper.php b/src/Phinx/Db/Adapter/AdapterWrapper.php index 09ee619dc..dd68ef5c5 100644 --- a/src/Phinx/Db/Adapter/AdapterWrapper.php +++ b/src/Phinx/Db/Adapter/AdapterWrapper.php @@ -466,6 +466,14 @@ public function castToBool($value): mixed return $this->getAdapter()->castToBool($value); } + /** + * @inheritDoc + */ + public function castToDate($value) + { + return $this->getAdapter()->castToDate($value); + } + /** * @return \PDO */ diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 28997e9b5..88313378d 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -334,6 +334,8 @@ public function insert(Table $table, array $row): void foreach ($row as $column => $value) { if (is_bool($value)) { $row[$column] = $this->castToBool($value); + } elseif (preg_match('/^[1-9][0-9]{3}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/', $value)) { + $row[$column] = $this->castToDate($value); } } @@ -432,6 +434,8 @@ public function bulkinsert(Table $table, array $rows): void continue; } elseif (is_bool($v)) { $vals[] = $this->castToBool($v); + } elseif (preg_match('/^[1-9][0-9]{3}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/', $v)) { + $vals[] = $this->castToDate($v); } else { $vals[] = $v; } @@ -517,8 +521,8 @@ public function migrated(MigrationInterface $migration, string $direction, strin $this->quoteColumnName('breakpoint'), $migration->getVersion(), substr($migration->getName(), 0, 100), - $startTime, - $endTime, + $this->castToDate($startTime), + $this->castToDate($endTime), $this->castToBool(false) ); @@ -683,6 +687,15 @@ public function castToBool($value): mixed return (bool)$value ? 1 : 0; } + /** + * @param string|null $value Date & time, in iso format (Y-m-d H:i:s) + * @return string + */ + public function castToDate(?string $value) + { + return $value; + } + /** * Retrieve a database connection attribute * @@ -711,6 +724,8 @@ protected function getDefaultValueDefinition(mixed $default, string|Literal|null $default = $this->getConnection()->quote($default); } elseif (is_bool($default)) { $default = $this->castToBool($default); + } elseif ($default !== null && $columnType === static::PHINX_TYPE_DATETIME) { + $default = $this->castToDate($default); } elseif ($default !== null && (string)$columnType === static::PHINX_TYPE_BOOLEAN) { $default = $this->castToBool((bool)$default); }