Skip to content

Commit 6e7ed8b

Browse files
committed
PHPORM-234 Convert dates in DB Query results
1 parent ebda1fa commit 6e7ed8b

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040)
77
* **BREAKING CHANGE** Make Query\Builder return objects instead of array to match Laravel behavior by @GromNaN in [#3107](https://github.com/mongodb/laravel-mongodb/pull/3107)
8+
* **BREAKING CHANGE** Convert BSON `UTCDateTime` objects into `Carbon` date in query results by @GromNaN in [#3119](https://github.com/mongodb/laravel-mongodb/pull/3119)
89

910
## [4.8.0] - 2024-08-27
1011

src/Query/Builder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Support\Arr;
1515
use Illuminate\Support\Carbon;
1616
use Illuminate\Support\Collection;
17+
use Illuminate\Support\Facades\Date;
1718
use Illuminate\Support\LazyCollection;
1819
use InvalidArgumentException;
1920
use LogicException;
@@ -1660,7 +1661,9 @@ private function aliasIdForResult(array|object $values): array|object
16601661
}
16611662

16621663
foreach ($values as $key => $value) {
1663-
if (is_array($value) || is_object($value)) {
1664+
if ($value instanceof UTCDateTime) {
1665+
$values[$key] = Date::instance($value->toDateTime());
1666+
} elseif (is_array($value) || $value instanceof stdClass) {
16641667
$values[$key] = $this->aliasIdForResult($value);
16651668
}
16661669
}
@@ -1673,7 +1676,9 @@ private function aliasIdForResult(array|object $values): array|object
16731676
}
16741677

16751678
foreach (get_object_vars($values) as $key => $value) {
1676-
if (is_array($value) || is_object($value)) {
1679+
if ($value instanceof UTCDateTime) {
1680+
$values->{$key} = Date::instance($value->toDateTime());
1681+
} elseif (is_array($value) || $value instanceof stdClass) {
16771682
$values->{$key} = $this->aliasIdForResult($value);
16781683
}
16791684
}

tests/QueryBuilderTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MongoDB\Laravel\Tests;
66

7+
use Carbon\Carbon;
78
use DateTime;
89
use DateTimeImmutable;
910
use Illuminate\Support\Facades\Date;
@@ -33,7 +34,6 @@
3334
use function md5;
3435
use function sort;
3536
use function strlen;
36-
use function strtotime;
3737

3838
class QueryBuilderTest extends TestCase
3939
{
@@ -676,27 +676,32 @@ public function testUpdateSubdocument()
676676
public function testDates()
677677
{
678678
DB::table('users')->insert([
679-
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00'))],
680-
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00'))],
681-
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse('1983-01-01 00:00:00.1'))],
682-
['name' => 'Frank White', 'birthday' => new UTCDateTime(Date::parse('1960-01-01 12:12:12.1'))],
679+
['name' => 'John Doe', 'birthday' => Date::parse('1980-01-01 00:00:00')],
680+
['name' => 'Robert Roe', 'birthday' => Date::parse('1982-01-01 00:00:00')],
681+
['name' => 'Mark Moe', 'birthday' => Date::parse('1983-01-01 00:00:00.1')],
682+
['name' => 'Frank White', 'birthday' => Date::parse('1975-01-01 12:12:12.1')],
683683
]);
684684

685685
$user = DB::table('users')
686-
->where('birthday', new UTCDateTime(Date::parse('1980-01-01 00:00:00')))
686+
->where('birthday', Date::parse('1980-01-01 00:00:00'))
687687
->first();
688688
$this->assertEquals('John Doe', $user->name);
689689

690690
$user = DB::table('users')
691-
->where('birthday', new UTCDateTime(Date::parse('1960-01-01 12:12:12.1')))
691+
->where('birthday', Date::parse('1975-01-01 12:12:12.1'))
692692
->first();
693+
693694
$this->assertEquals('Frank White', $user->name);
695+
$this->assertInstanceOf(Carbon::class, $user->birthday);
696+
$this->assertSame('1975-01-01 12:12:12.100000', $user->birthday->format('Y-m-d H:i:s.u'));
694697

695698
$user = DB::table('users')->where('birthday', '=', new DateTime('1980-01-01 00:00:00'))->first();
696699
$this->assertEquals('John Doe', $user->name);
700+
$this->assertInstanceOf(Carbon::class, $user->birthday);
701+
$this->assertSame('1980-01-01 00:00:00.000000', $user->birthday->format('Y-m-d H:i:s.u'));
697702

698-
$start = new UTCDateTime(1000 * strtotime('1950-01-01 00:00:00'));
699-
$stop = new UTCDateTime(1000 * strtotime('1981-01-01 00:00:00'));
703+
$start = Carbon::parse('1950-01-01 00:00:00');
704+
$stop = Carbon::parse('1981-01-01 00:00:00');
700705

701706
$users = DB::table('users')->whereBetween('birthday', [$start, $stop])->get();
702707
$this->assertCount(2, $users);

0 commit comments

Comments
 (0)