Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force typing for Postgresql::date_trunc so it can be used in a condition #415

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Query/Postgresql/DateTrunc.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function parse(Parser $parser): void
public function getSql(SqlWalker $sqlWalker): string
{
return sprintf(
'DATE_TRUNC(%s, %s)',
'DATE_TRUNC(%s::text, %s::timestamp)',
$this->fieldText->dispatch($sqlWalker),
$this->fieldTimestamp->dispatch($sqlWalker)
);
Expand Down
16 changes: 15 additions & 1 deletion tests/Query/Postgresql/DateTruncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace DoctrineExtensions\Tests\Query\Postgresql;

use DateTime;
use Doctrine\ORM\QueryBuilder;
use DoctrineExtensions\Tests\Entities\Date;
use DoctrineExtensions\Tests\Query\PostgresqlTestCase;

class DateTruncTest extends PostgresqlTestCase
Expand All @@ -14,7 +16,19 @@ public function testDateTrunc(): void
->select("date_trunc('YEAR', dt.created)")
->from('DoctrineExtensions\Tests\Entities\Date', 'dt');

$expected = "SELECT DATE_TRUNC('YEAR', d0_.created) AS sclr_0 FROM Date d0_";
$expected = "SELECT DATE_TRUNC('YEAR'::text, d0_.created::timestamp) AS sclr_0 FROM Date d0_";

$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL());
}

public function testDateTruncCondition(): void
{
$queryBuilder = $this->entityManager->getRepository(Date::class)
->createQueryBuilder('dt')
->where("date_trunc('YEAR', dt.created) = date_trunc('YEAR', :date)")
->setParameter('date', new DateTime('2010-01-01'));

$expected = "SELECT d0_.id AS id_0, d0_.created AS created_1 FROM Date d0_ WHERE DATE_TRUNC('YEAR'::text, d0_.created::timestamp) = DATE_TRUNC('YEAR'::text, ?::timestamp)";

$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL());
}
Expand Down
Loading