Skip to content

Commit 6442a4e

Browse files
authored
Merge pull request #191 from homersimpsons/fix/find-by-datetime
DateTime filter (#189): Add test case
2 parents 347a348 + 443e451 commit 6442a4e

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Schema\Schema;
99
use Doctrine\DBAL\Schema\Table;
1010
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
11+
use Doctrine\DBAL\Types\Type;
1112
use JsonSerializable;
1213
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
1314
use PhpParser\Comment\Doc;
@@ -1291,7 +1292,17 @@ private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespa
12911292
foreach ($elements as $element) {
12921293
$params[] = $element->getParamAnnotation();
12931294
if ($element instanceof ScalarBeanPropertyDescriptor) {
1294-
$filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getSafeVariableName().",\n";
1295+
$typeName = $element->getDatabaseType()->getName();
1296+
if ($typeName === Type::DATETIME_IMMUTABLE) {
1297+
$filterArrayCode .= sprintf(
1298+
" %s => \$this->tdbmService->getConnection()->convertToDatabaseValue(%s, %s),\n",
1299+
var_export($element->getColumnName(), true),
1300+
$element->getSafeVariableName(),
1301+
var_export($typeName, true)
1302+
);
1303+
} else {
1304+
$filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getSafeVariableName().",\n";
1305+
}
12951306
} elseif ($element instanceof ObjectBeanPropertyDescriptor) {
12961307
$foreignKey = $element->getForeignKey();
12971308
$columns = SafeFunctions::arrayCombine($foreignKey->getLocalColumns(), $foreignKey->getForeignColumns());

src/Utils/ScalarBeanPropertyDescriptor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public function getPhpType(): string
7373
return TDBMDaoGenerator::dbalTypeToPhpType($type);
7474
}
7575

76+
/**
77+
* Returns the Database type for the property
78+
*
79+
* @return Type
80+
*/
81+
public function getDatabaseType(): Type
82+
{
83+
return $this->column->getType();
84+
}
85+
7686
/**
7787
* Returns true if the property is compulsory (and therefore should be fetched in the constructor).
7888
*

tests/TDBMAbstractServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private static function initSchema(Connection $connection): void
266266
}
267267

268268
$db->table('person')
269-
->column('modified_at')->datetime()->null()
269+
->column('modified_at')->datetime()->null()->index()
270270
->column('order')->integer()->null();
271271

272272

tests/TDBMDaoGeneratorTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao;
7777
use TheCodingMachine\TDBM\Test\Dao\InheritedObjectDao;
7878
use TheCodingMachine\TDBM\Test\Dao\NodeDao;
79+
use TheCodingMachine\TDBM\Test\Dao\PersonDao;
7980
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao;
8081
use TheCodingMachine\TDBM\Test\Dao\RoleDao;
8182
use TheCodingMachine\TDBM\Test\Dao\StateDao;
@@ -747,8 +748,9 @@ public function testFindFromRawSqlOrderByUserCount(): void
747748
$countryDao = new TestCountryDao($this->tdbmService);
748749
$countries = $countryDao->getCountriesByUserCount();
749750

750-
$this->assertCount(4, $countries);
751-
for ($i = 1; $i < count($countries); $i++) {
751+
$nbCountries = 4;
752+
$this->assertCount($nbCountries, $countries);
753+
for ($i = 1; $i < $nbCountries; $i++) {
752754
$this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count());
753755
}
754756
}
@@ -2169,4 +2171,11 @@ public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): v
21692171
$artist->getChildrenByArtistsRelations(); // auto-pivot relationship
21702172
$this->assertEquals(1, 1);
21712173
}
2174+
2175+
public function testFindByDateTime(): void
2176+
{
2177+
$personDao = new PersonDao($this->tdbmService);
2178+
$personDao->findByModifiedAt(new \DateTimeImmutable())->count();
2179+
$this->assertTrue(true);
2180+
}
21722181
}

0 commit comments

Comments
 (0)