I am trying to compare datetime values using Doctrine.
I have a Doctrine query that looks like this:
$interval = new \DateInterval('PT'. 2 .'H');
$oldestAllowedDateTime = new \DateTime();
$oldestAllowedDateTime = $oldestAllowedDateTime->sub( $interval );
$queryBuilder = $repository->createQueryBuilder('pn');
$queryBuilder = $queryBuilder->select('notification')
->from('AppBundle\Entity\PushNotification','notification')
->where('pn.isSent IS NULL OR pn.isSent = 0')
->andwhere('pn.sendDate > :oldestAllowedDateTime')
->orderBy('pn.sendDate', 'DESC')
;
$queryBuilder->setParameter('oldestAllowedDateTime',$oldestAllowedDateTime);
$result = $queryBuilder->getQuery()->getResult();
... and it is creating SQL that looks like this:
SELECT p0_.id AS id_0,
p0_.content AS content_1, p0_.last_offset AS last_offset_2,
p0_.failure_count AS failure_count_3, p0_.is_sent AS is_sent_4,
p0_.send_date AS send_date_5 FROM push_notification p1_,
push_notification p0_ WHERE (p1_.is_sent IS NULL OR p1_.is_sent = 0)
AND p1_.send_date > ? ORDER BY p1_.send_date DESC
Doctrine is returning all records, including ones that are older than the datetime I specify. That makes sense in a way, since Doctrine appears to be setting up an "always true" scenario involving the timestamp comparison I want to do.
Is there a simple way to make Doctrine only return the records that are newer than my oldestAllowedDateTime value?
====
Edit: Here is the send_date content from my database.
mysql> SELECT send_date FROM push_notification;
+---------------------+
| send_date |
+---------------------+
| 2018-01-24 05:51:21 |
| 2018-01-24 11:44:30 |
+---------------------+
2 rows in set (0.00 sec)