Skip to content

Commit bb1edc5

Browse files
committed
PHPORM-60 Native support for whereTime
1 parent f6ac96d commit bb1edc5

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
@@ -16,7 +16,7 @@ All notable changes to this project will be documented in this file.
1616
- Remove `Query\Builder::whereAll($column, $values)`. Use `Query\Builder::where($column, 'all', $values)` instead. [#16](https://github.com/GromNaN/laravel-mongodb-private/pull/16) by [@GromNaN](https://github.com/GromNaN).
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-private/pull/21) by [@GromNaN](https://github.com/GromNaN).
1818
- Support `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).
19-
- Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear` to use MongoDB operators [#2376](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu)
19+
- 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)
2020

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

src/Query/Builder.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,10 +1304,16 @@ protected function compileWhereTime(array $where): array
13041304
{
13051305
extract($where);
13061306

1307-
$where['operator'] = $operator;
1308-
$where['value'] = $value;
1309-
1310-
return $this->compileWhereBasic($where);
1307+
return [
1308+
'$expr' => [
1309+
'$'.$operator => [
1310+
[
1311+
'$dateToString' => ['date' => '$'.$column, 'format' => '%H:%M:%S'],
1312+
],
1313+
$value,
1314+
],
1315+
],
1316+
];
13111317
}
13121318

13131319
/**

tests/Query/BuilderTest.php

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

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