Skip to content

Commit

Permalink
fix timestampablebehavior not using datetimeclass
Browse files Browse the repository at this point in the history
  • Loading branch information
smhg committed Sep 26, 2024
1 parent b35918b commit 3c47362
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ protected function getColumnConstant(string $columnName, AbstractOMBuilder $buil
public function preUpdate(AbstractOMBuilder $builder): string
{
if ($this->withUpdatedAt()) {
$valueSource = strtoupper($this->getTable()->getColumn($this->getParameter('update_column'))->getType()) === 'INTEGER'
$updateColumn = $this->getTable()->getColumn($this->getParameter('update_column'));
$dateTimeClass = $builder->getDateTimeClass($updateColumn);

$valueSource = strtoupper($updateColumn->getType()) === 'INTEGER'
? 'time()'
: '\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision()';
: "\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision(null, '$dateTimeClass')";

return 'if ($this->isModified() && !$this->isColumnModified(' . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "({$valueSource});
Expand All @@ -123,22 +126,34 @@ public function preUpdate(AbstractOMBuilder $builder): string
public function preInsert(AbstractOMBuilder $builder): string
{
$script = '$time = time();
$highPrecision = \\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision();';
$mtime = PropelDateTime::formatMicrotime(microtime(true));';

if ($this->withCreatedAt()) {
$valueSource = strtoupper($this->getTable()->getColumn($this->getParameter('create_column'))->getType()) === 'INTEGER'
$createColumn = $this->getTable()->getColumn($this->getParameter('create_column'));
$dateTimeClass = $builder->getDateTimeClass($createColumn);

$script .= "
\$highPrecisionCreate = PropelDateTime::createHighPrecision(\$mtime, '$dateTimeClass');";

$valueSource = strtoupper($createColumn->getType()) === 'INTEGER'
? '$time'
: '$highPrecision';
: '$highPrecisionCreate';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('create_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('create_column') . "({$valueSource});
}";
}

if ($this->withUpdatedAt()) {
$valueSource = strtoupper($this->getTable()->getColumn($this->getParameter('update_column'))->getType()) === 'INTEGER'
$updateColumn = $this->getTable()->getColumn($this->getParameter('update_column'));
$dateTimeClass = $builder->getDateTimeClass($updateColumn);

$script .= "
\$highPrecisionUpdate = PropelDateTime::createHighPrecision(\$mtime, '$dateTimeClass');";

$valueSource = strtoupper($updateColumn->getType()) === 'INTEGER'
? '$time'
: '$highPrecision';
: '$highPrecisionUpdate';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "({$valueSource});
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Builder/Om/ObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7359,7 +7359,7 @@ protected function addMagicCall(string &$script): void
*
* @return string
*/
protected function getDateTimeClass(Column $column): string
public function getDateTimeClass(Column $column): string
{
if (PropelTypes::isPhpObjectType($column->getPhpType())) {
return $column->getPhpType();
Expand Down
22 changes: 16 additions & 6 deletions src/Propel/Runtime/Util/PropelDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,41 @@ protected static function isTimestamp($value): bool
*
* @throws \InvalidArgumentException
*
* @return \DateTime
* @return mixed An instance of $dateTimeClass
*/
public static function createHighPrecision(?string $time = null): DateTime
public static function createHighPrecision(?string $time = null, string $dateTimeClass = 'DateTime'): DateTimeInterface
{
$dateTime = DateTime::createFromFormat('U.u', $time ?: self::getMicrotime());
$dateTime = $dateTimeClass::createFromFormat('U.u', $time ?: self::getMicrotime());
if ($dateTime === false) {
throw new InvalidArgumentException('Cannot create a datetime object from `' . $time . '`');
}

$dateTime->setTimeZone(new DateTimeZone(date_default_timezone_get()));
$dateTime = $dateTime->setTimeZone(new DateTimeZone(date_default_timezone_get()));

return $dateTime;
}

/**
* Get the current microtime with milliseconds. Making sure that the decimal point separator is always ".", ignoring
* Format the output of microtime(true) making sure that the decimal point separator is always ".", ignoring
* what is set with the current locale. Otherwise, self::createHighPrecision would return false.
*
* @return string
*/
public static function formatMicrotime(float $mtime): string
{
return number_format($mtime, 6, '.', '');
}

/**
* Get the current microtime with milliseconds.
*
* @return string
*/
public static function getMicrotime(): string
{
$mtime = microtime(true);

return number_format($mtime, 6, '.', '');
return self::formatMicrotime($mtime);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Propel/Tests/Runtime/Util/PropelDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
use Propel\Runtime\Exception\PropelException;
Expand Down Expand Up @@ -245,11 +246,11 @@ public function testIsTimestamp()
public function testCreateHighPrecision()
{
$createHP = PropelDateTime::createHighPrecision();
$this->assertInstanceOf(DateTime::class, $createHP);
$this->assertInstanceOf(DateTimeInterface::class, $createHP);

setlocale(LC_ALL, 'de_DE.UTF-8');
$createHP = PropelDateTime::createHighPrecision();
$this->assertInstanceOf(DateTime::class, $createHP);
$this->assertInstanceOf(DateTimeInterface::class, $createHP);
}

/**
Expand Down

0 comments on commit 3c47362

Please sign in to comment.