From a437f48258c574040be38fc77699cc6778f375b1 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 16 Jan 2020 17:05:55 +0100 Subject: [PATCH 1/2] DateTime filter (#189): Add test case --- tests/TDBMAbstractServiceTest.php | 2 +- tests/TDBMDaoGeneratorTest.php | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/TDBMAbstractServiceTest.php b/tests/TDBMAbstractServiceTest.php index 9984488e..97608fda 100644 --- a/tests/TDBMAbstractServiceTest.php +++ b/tests/TDBMAbstractServiceTest.php @@ -266,7 +266,7 @@ private static function initSchema(Connection $connection): void } $db->table('person') - ->column('modified_at')->datetime()->null() + ->column('modified_at')->datetime()->null()->index() ->column('order')->integer()->null(); diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index 14ed805e..7d00df14 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -76,6 +76,7 @@ use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao; use TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao; use TheCodingMachine\TDBM\Test\Dao\NodeDao; +use TheCodingMachine\TDBM\Test\Dao\PersonDao; use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao; use TheCodingMachine\TDBM\Test\Dao\RoleDao; use TheCodingMachine\TDBM\Test\Dao\StateDao; @@ -747,8 +748,9 @@ public function testFindFromRawSqlOrderByUserCount(): void $countryDao = new TestCountryDao($this->tdbmService); $countries = $countryDao->getCountriesByUserCount(); - $this->assertCount(4, $countries); - for ($i = 1; $i < count($countries); $i++) { + $nbCountries = 4; + $this->assertCount($nbCountries, $countries); + for ($i = 1; $i < $nbCountries; $i++) { $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count()); } } @@ -2169,4 +2171,11 @@ public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): v $artist->getChildrenByArtistsRelations(); // auto-pivot relationship $this->assertEquals(1, 1); } + + public function testFindByDateTime(): void + { + $personDao = new PersonDao($this->tdbmService); + $personDao->findByModifiedAt(new \DateTimeImmutable('+1 year'))->count(); + $this->assertTrue(true); + } } From 443e451239080ec3fc2f933d8ebea968b9ad65a8 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 16 Jan 2020 17:28:39 +0100 Subject: [PATCH 2/2] DateTime filter (#189): Convert \DateTimeImmutable to Database value --- src/Utils/BeanDescriptor.php | 13 ++++++++++++- src/Utils/ScalarBeanPropertyDescriptor.php | 10 ++++++++++ tests/TDBMDaoGeneratorTest.php | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Utils/BeanDescriptor.php b/src/Utils/BeanDescriptor.php index 0aa828ab..e4b5c250 100644 --- a/src/Utils/BeanDescriptor.php +++ b/src/Utils/BeanDescriptor.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\ForeignKeyConstraint; +use Doctrine\DBAL\Types\Type; use JsonSerializable; use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; use PhpParser\Comment\Doc; @@ -1291,7 +1292,17 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa foreach ($elements as $element) { $params[] = $element->getParamAnnotation(); if ($element instanceof ScalarBeanPropertyDescriptor) { - $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getSafeVariableName().",\n"; + $typeName = $element->getDatabaseType()->getName(); + if ($typeName === Type::DATETIME_IMMUTABLE) { + $filterArrayCode .= sprintf( + " %s => \$this->tdbmService->getConnection()->convertToDatabaseValue(%s, %s),\n", + var_export($element->getColumnName(), true), + $element->getSafeVariableName(), + var_export($typeName, true) + ); + } else { + $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getSafeVariableName().",\n"; + } } elseif ($element instanceof ObjectBeanPropertyDescriptor) { $foreignKey = $element->getForeignKey(); $columns = SafeFunctions::arrayCombine($foreignKey->getLocalColumns(), $foreignKey->getForeignColumns()); diff --git a/src/Utils/ScalarBeanPropertyDescriptor.php b/src/Utils/ScalarBeanPropertyDescriptor.php index 66daf242..9bfbd360 100644 --- a/src/Utils/ScalarBeanPropertyDescriptor.php +++ b/src/Utils/ScalarBeanPropertyDescriptor.php @@ -73,6 +73,16 @@ public function getPhpType(): string return TDBMDaoGenerator::dbalTypeToPhpType($type); } + /** + * Returns the Database type for the property + * + * @return Type + */ + public function getDatabaseType(): Type + { + return $this->column->getType(); + } + /** * Returns true if the property is compulsory (and therefore should be fetched in the constructor). * diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index 7d00df14..86a5bbb9 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -2175,7 +2175,7 @@ public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): v public function testFindByDateTime(): void { $personDao = new PersonDao($this->tdbmService); - $personDao->findByModifiedAt(new \DateTimeImmutable('+1 year'))->count(); + $personDao->findByModifiedAt(new \DateTimeImmutable())->count(); $this->assertTrue(true); } }