Skip to content

Commit

Permalink
DateTime filter (#189): Convert \DateTimeImmutable to Database value
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Feb 7, 2020
1 parent a437f48 commit 443e451
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Utils/BeanDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions src/Utils/ScalarBeanPropertyDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down
2 changes: 1 addition & 1 deletion tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 443e451

Please sign in to comment.