Skip to content

Commit e33764e

Browse files
DavpyuGromNaN
authored andcommitted
Fix whereDate, whereMonth, whereYear, whereTime to use $expr and respective query rather than using basic comparison
1 parent 6c7df45 commit e33764e

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +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).
2021

2122
## [3.9.2] - 2022-09-01
2223

src/Query/Builder.php

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Query\Builder as BaseBuilder;
99
use Illuminate\Database\Query\Expression;
1010
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Carbon;
1112
use Illuminate\Support\Collection;
1213
use Illuminate\Support\LazyCollection;
1314
use Jenssegers\Mongodb\Connection;
@@ -1182,10 +1183,45 @@ protected function compileWhereDate(array $where): array
11821183
{
11831184
extract($where);
11841185

1185-
$where['operator'] = $operator;
1186-
$where['value'] = $value;
1186+
$startOfDay = new UTCDateTime(Carbon::parse($value)->startOfDay());
1187+
$endOfDay = new UTCDateTime(Carbon::parse($value)->endOfDay());
11871188

1188-
return $this->compileWhereBasic($where);
1189+
$operator = $this->conversion[$operator];
1190+
1191+
return match($operator) {
1192+
'=' => [
1193+
$column => [
1194+
'$gte' => $startOfDay,
1195+
'$lte' => $endOfDay,
1196+
],
1197+
],
1198+
'$ne' => [
1199+
$column => [
1200+
'$gt' => $endOfDay,
1201+
'$lt' => $startOfDay,
1202+
],
1203+
],
1204+
'$lt' => [
1205+
$column => [
1206+
'$lt' => $startOfDay,
1207+
],
1208+
],
1209+
'$gt' => [
1210+
$column => [
1211+
'$gt' => $endOfDay,
1212+
],
1213+
],
1214+
'$lte' => [
1215+
$column => [
1216+
'$lte' => $endOfDay,
1217+
],
1218+
],
1219+
'$gte' => [
1220+
$column => [
1221+
'$gte' => $startOfDay,
1222+
],
1223+
],
1224+
};
11891225
}
11901226

11911227
/**
@@ -1196,10 +1232,19 @@ protected function compileWhereMonth(array $where): array
11961232
{
11971233
extract($where);
11981234

1199-
$where['operator'] = $operator;
1200-
$where['value'] = $value;
1235+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1236+
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
12011237

1202-
return $this->compileWhereBasic($where);
1238+
return [
1239+
'$expr' => [
1240+
$operator => [
1241+
[
1242+
'$month' => '$'.$column
1243+
],
1244+
$value,
1245+
],
1246+
],
1247+
];
12031248
}
12041249

12051250
/**
@@ -1210,10 +1255,19 @@ protected function compileWhereDay(array $where): array
12101255
{
12111256
extract($where);
12121257

1213-
$where['operator'] = $operator;
1214-
$where['value'] = $value;
1258+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1259+
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
12151260

1216-
return $this->compileWhereBasic($where);
1261+
return [
1262+
'$expr' => [
1263+
$operator => [
1264+
[
1265+
'$dayOfMonth' => '$'.$column
1266+
],
1267+
$value,
1268+
],
1269+
],
1270+
];
12171271
}
12181272

12191273
/**
@@ -1224,10 +1278,18 @@ protected function compileWhereYear(array $where): array
12241278
{
12251279
extract($where);
12261280

1227-
$where['operator'] = $operator;
1228-
$where['value'] = $value;
1281+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
12291282

1230-
return $this->compileWhereBasic($where);
1283+
return [
1284+
'$expr' => [
1285+
$operator => [
1286+
[
1287+
'$year' => '$'.$column
1288+
],
1289+
$value
1290+
],
1291+
],
1292+
];
12311293
}
12321294

12331295
/**

tests/Models/Birthday.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
*
1212
* @property string $name
1313
* @property string $birthday
14-
* @property string $day
15-
* @property string $month
16-
* @property string $year
1714
* @property string $time
1815
*/
1916
class Birthday extends Eloquent
2017
{
2118
protected $connection = 'mongodb';
2219
protected $collection = 'birthday';
23-
protected $fillable = ['name', 'birthday', 'day', 'month', 'year', 'time'];
20+
protected $fillable = ['name', 'birthday', 'time'];
2421
}

0 commit comments

Comments
 (0)