diff --git a/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php b/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php index 6c84c225fb..e6c4d53b3f 100644 --- a/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php +++ b/src/Propel/Generator/Behavior/Timestampable/TimestampableBehavior.php @@ -27,8 +27,17 @@ class TimestampableBehavior extends Behavior 'update_column' => 'updated_at', 'disable_created_at' => 'false', 'disable_updated_at' => 'false', + 'is_timestamp' => 'true', ]; + /** + * @return bool + */ + protected function isTimestamp(): bool + { + return $this->booleanValue($this->getParameter('is_timestamp')); + } + /** * @return bool */ @@ -57,13 +66,13 @@ public function modifyTable(): void if ($this->withCreatedAt() && !$table->hasColumn($this->getParameter('create_column'))) { $table->addColumn([ 'name' => $this->getParameter('create_column'), - 'type' => 'TIMESTAMP', + 'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME', ]); } if ($this->withUpdatedAt() && !$table->hasColumn($this->getParameter('update_column'))) { $table->addColumn([ 'name' => $this->getParameter('update_column'), - 'type' => 'TIMESTAMP', + 'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME', ]); } } @@ -106,7 +115,7 @@ public function preUpdate(AbstractOMBuilder $builder): string : '\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision()'; return 'if ($this->isModified() && !$this->isColumnModified(' . $this->getColumnConstant('update_column', $builder) . ")) { - \$this->" . $this->getColumnSetter('update_column') . "(${valueSource}); + \$this->" . $this->getColumnSetter('update_column') . "($valueSource); }"; } @@ -131,7 +140,7 @@ public function preInsert(AbstractOMBuilder $builder): string : '$highPrecision'; $script .= " if (!\$this->isColumnModified(" . $this->getColumnConstant('create_column', $builder) . ")) { - \$this->" . $this->getColumnSetter('create_column') . "(${valueSource}); + \$this->" . $this->getColumnSetter('create_column') . "($valueSource); }"; } @@ -141,7 +150,7 @@ public function preInsert(AbstractOMBuilder $builder): string : '$highPrecision'; $script .= " if (!\$this->isColumnModified(" . $this->getColumnConstant('update_column', $builder) . ")) { - \$this->" . $this->getColumnSetter('update_column') . "(${valueSource}); + \$this->" . $this->getColumnSetter('update_column') . "($valueSource); }"; } diff --git a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php index 035469a5cb..ca976e4f87 100644 --- a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php @@ -356,4 +356,54 @@ public function testDisableCreatedAt() $this->assertEquals(1, $obj->save()); $this->assertNotNull($obj->getUpdatedAt()); } + + /** + * @return void + */ + public function testWithDatetimeSqlType() + { + $schema = << + + + + + + + +
+ +EOF; + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $builder->build(); + $sql = $builder->getSQL(); + $this->assertStringContainsString('created_at DATETIME,', $sql, 'created_at column should be DATETIME'); + $this->assertStringContainsString('updated_at DATETIME,', $sql, 'updated_at column should be DATETIME'); + } + + /** + * @return void + */ + public function testWithTimestampSqlType() + { + $schema = << + + + + + + + +
+ +EOF; + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $builder->build(); + $sql = $builder->getSQL(); + $this->assertStringContainsString('created_at TIMESTAMP,', $sql, 'created_at column should be TIMESTAMP'); + $this->assertStringContainsString('updated_at TIMESTAMP,', $sql, 'updated_at column should be TIMESTAMP'); + } } diff --git a/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php b/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php index 3350558526..8d33536465 100644 --- a/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php +++ b/tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php @@ -314,6 +314,7 @@ public function testBehaviors() 'update_column' => 'updated_on', 'disable_created_at' => 'false', 'disable_updated_at' => 'false', + 'is_timestamp' => 'true' ], ]; $this->assertEquals( diff --git a/tests/Propel/Tests/Generator/Model/BehaviorTest.php b/tests/Propel/Tests/Generator/Model/BehaviorTest.php index 72ca41b301..61b93f4cdd 100644 --- a/tests/Propel/Tests/Generator/Model/BehaviorTest.php +++ b/tests/Propel/Tests/Generator/Model/BehaviorTest.php @@ -155,7 +155,8 @@ public function testSchemaReader() 'leParameterList' => [ ['leListItem1Value' => 'leValue1'], ['leListItem2Value1' => 'leValue2.1', 'leListItem2Value2' => 'leValue2.2'], - ] + ], + 'is_timestamp' => 'true' ]; $this->assertEquals($expectedParameters, $behavior->getParameters(), 'SchemaReader sets the behavior parameters correctly'); } diff --git a/tests/Propel/Tests/Resources/blog-database.xml b/tests/Propel/Tests/Resources/blog-database.xml index 60e0a508da..6bc91295ca 100644 --- a/tests/Propel/Tests/Resources/blog-database.xml +++ b/tests/Propel/Tests/Resources/blog-database.xml @@ -23,6 +23,7 @@ + diff --git a/tests/Propel/Tests/Resources/blog-schema.xml b/tests/Propel/Tests/Resources/blog-schema.xml index acf1143130..050550a487 100644 --- a/tests/Propel/Tests/Resources/blog-schema.xml +++ b/tests/Propel/Tests/Resources/blog-schema.xml @@ -36,6 +36,7 @@ +