Skip to content

Commit adfcb94

Browse files
committed
PHPORM-60 Native support for whereTime
1 parent 16a50c6 commit adfcb94

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ All notable changes to this project will be documented in this file.
1717
- Fix validation of unique values when the validated value is found as part of an existing value. [#21](https://github.com/GromNaN/laravel-mongodb/pull/21) by [@GromNaN](https://github.com/GromNaN).
1818
- Support `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb/pull/17) by [@GromNaN](https://github.com/GromNaN).
1919
- Change signature of `Query\Builder::__constructor` to match the parent class [#26](https://github.com/GromNaN/laravel-mongodb-private/pull/26) by [@GromNaN](https://github.com/GromNaN).
20-
- Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2376](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu) and [@GromNaN](https://github.com/GromNaN).
20+
- Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2570](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu) and [@GromNaN](https://github.com/GromNaN).
2121

2222
## [3.9.2] - 2022-09-01
2323

src/Query/Builder.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,10 +1295,16 @@ protected function compileWhereTime(array $where): array
12951295
{
12961296
extract($where);
12971297

1298-
$where['operator'] = $operator;
1299-
$where['value'] = $value;
1300-
1301-
return $this->compileWhereBasic($where);
1298+
return [
1299+
'$expr' => [
1300+
'$'.$operator => [
1301+
[
1302+
'$dateToString' => ['date' => '$'.$column, 'format' => '%H:%M:%S'],
1303+
],
1304+
$value,
1305+
],
1306+
],
1307+
];
13021308
}
13031309

13041310
/**

tests/Query/BuilderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,26 @@ function (Builder $builder) {
758758
fn (Builder $builder) => $builder->whereYear('created_at', '>', '2023'),
759759
];
760760

761+
yield 'where time' => [
762+
['find' => [['$expr' => [
763+
'$eq' => [
764+
['$dateToString' => ['date' => '$created_at', 'format' => '%H:%M:%S']],
765+
'10:11:12',
766+
],
767+
]], []]],
768+
fn (Builder $builder) => $builder->whereTime('created_at', '10:11:12'),
769+
];
770+
771+
yield 'where time >' => [
772+
['find' => [['$expr' => [
773+
'$gt' => [
774+
['$dateToString' => ['date' => '$created_at', 'format' => '%H:%M:%S']],
775+
'10:11:12',
776+
],
777+
]], []]],
778+
fn (Builder $builder) => $builder->whereTime('created_at', '>', '10:11:12'),
779+
];
780+
761781
/** @see DatabaseQueryBuilderTest::testBasicSelectDistinct */
762782
yield 'distinct' => [
763783
['distinct' => ['foo', [], []]],

tests/QueryTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function setUp(): void
2525
User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']);
2626
User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']);
2727
User::create(['name' => 'Error', 'age' => null, 'title' => null]);
28-
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2020-04-10 10:53:11'), 'time' => '10:53:11']);
29-
Birthday::create(['name' => 'Jane Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:12'), 'time' => '10:53:12']);
30-
Birthday::create(['name' => 'Harry Hoe', 'birthday' => new DateTimeImmutable('2021-05-11 10:53:13'), 'time' => '10:53:13']);
31-
Birthday::create(['name' => 'Robert Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:14'), 'time' => '10:53:14']);
32-
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:15'), 'time' => '10:53:15']);
33-
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2022-05-12 10:53:16'), 'time' => '10:53:16']);
28+
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2020-04-10 10:53:11')]);
29+
Birthday::create(['name' => 'Jane Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:12')]);
30+
Birthday::create(['name' => 'Harry Hoe', 'birthday' => new DateTimeImmutable('2021-05-11 10:53:13')]);
31+
Birthday::create(['name' => 'Robert Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:14')]);
32+
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:15')]);
33+
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2022-05-12 10:53:16')]);
3434
Birthday::create(['name' => 'Boo']);
3535
}
3636

@@ -267,11 +267,17 @@ public function testWhereYear(): void
267267

268268
public function testWhereTime(): void
269269
{
270-
$time = Birthday::whereTime('time', '10:53:11')->get();
270+
$time = Birthday::whereTime('birthday', '10:53:11')->get();
271271
$this->assertCount(1, $time);
272272

273-
$time = Birthday::whereTime('time', '>=', '10:53:14')->get();
273+
$time = Birthday::whereTime('birthday', '>=', '10:53:14')->get();
274274
$this->assertCount(3, $time);
275+
276+
$time = Birthday::whereTime('birthday', '!=', '10:53:14')->get();
277+
$this->assertCount(6, $time);
278+
279+
$time = Birthday::whereTime('birthday', '<', '10:53:12')->get();
280+
$this->assertCount(2, $time);
275281
}
276282

277283
public function testOrder(): void

0 commit comments

Comments
 (0)