Here is my field im trying to get with doctrine
{
"_id" : ObjectId("5512f2ae73d151bb528b4589"),
"name" : "Strandby-1",
"enddate" : ISODate("2015-03-31T22:00:00.000Z"),
"startdate" : ISODate("2015-03-24T23:00:00.000Z")
}
Now Im trying to do find it inside my repository
$time = new \DateTime();
$query = $this->createQueryBuilder();
$query
->field('startdate')->gte($time)
->field('enddate')->lte($time)
;
return $query->getQuery()->getSingleResult();
But im just getting null
I always tried where $time = new \MongoDate(); but still getting null
Doctrine also converts DateTime, string time or timestamp to MongoDate by itself, look at doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Types\DateType.php
public static function getDateTime($value)
{
$datetime = false;
$exception = null;
if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
return $value;
...
Which is called in
public function convertToDatabaseValue($value)
{
if ($value === null || $value instanceof \MongoDate) {
return $value;
}
$datetime = self::getDateTime($value);
So the problem is not in a date type. Your problem is in logic.
You are trying to get an object with startdate >= now >= enddate
Try this:
->field('startdate')->lte($time)
->field('enddate')->gte($time)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With