Skip to content

Commit 27e1a38

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

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
@@ -16,6 +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)
1920

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

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 Illuminate\Support\Str;
@@ -1191,10 +1192,45 @@ protected function compileWhereDate(array $where): array
11911192
{
11921193
extract($where);
11931194

1194-
$where['operator'] = $operator;
1195-
$where['value'] = $value;
1195+
$startOfDay = new UTCDateTime(Carbon::parse($value)->startOfDay());
1196+
$endOfDay = new UTCDateTime(Carbon::parse($value)->endOfDay());
11961197

1197-
return $this->compileWhereBasic($where);
1198+
$operator = $this->conversion[$operator];
1199+
1200+
return match($operator) {
1201+
'=' => [
1202+
$column => [
1203+
'$gte' => $startOfDay,
1204+
'$lte' => $endOfDay,
1205+
],
1206+
],
1207+
'$ne' => [
1208+
$column => [
1209+
'$gt' => $endOfDay,
1210+
'$lt' => $startOfDay,
1211+
],
1212+
],
1213+
'$lt' => [
1214+
$column => [
1215+
'$lt' => $startOfDay,
1216+
],
1217+
],
1218+
'$gt' => [
1219+
$column => [
1220+
'$gt' => $endOfDay,
1221+
],
1222+
],
1223+
'$lte' => [
1224+
$column => [
1225+
'$lte' => $endOfDay,
1226+
],
1227+
],
1228+
'$gte' => [
1229+
$column => [
1230+
'$gte' => $startOfDay,
1231+
],
1232+
],
1233+
};
11981234
}
11991235

12001236
/**
@@ -1205,10 +1241,19 @@ protected function compileWhereMonth(array $where): array
12051241
{
12061242
extract($where);
12071243

1208-
$where['operator'] = $operator;
1209-
$where['value'] = $value;
1244+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1245+
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
12101246

1211-
return $this->compileWhereBasic($where);
1247+
return [
1248+
'$expr' => [
1249+
$operator => [
1250+
[
1251+
'$month' => '$'.$column
1252+
],
1253+
$value,
1254+
],
1255+
],
1256+
];
12121257
}
12131258

12141259
/**
@@ -1219,10 +1264,19 @@ protected function compileWhereDay(array $where): array
12191264
{
12201265
extract($where);
12211266

1222-
$where['operator'] = $operator;
1223-
$where['value'] = $value;
1267+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1268+
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
12241269

1225-
return $this->compileWhereBasic($where);
1270+
return [
1271+
'$expr' => [
1272+
$operator => [
1273+
[
1274+
'$dayOfMonth' => '$'.$column
1275+
],
1276+
$value,
1277+
],
1278+
],
1279+
];
12261280
}
12271281

12281282
/**
@@ -1233,10 +1287,18 @@ protected function compileWhereYear(array $where): array
12331287
{
12341288
extract($where);
12351289

1236-
$where['operator'] = $operator;
1237-
$where['value'] = $value;
1290+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
12381291

1239-
return $this->compileWhereBasic($where);
1292+
return [
1293+
'$expr' => [
1294+
$operator => [
1295+
[
1296+
'$year' => '$'.$column
1297+
],
1298+
$value
1299+
],
1300+
],
1301+
];
12401302
}
12411303

12421304
/**

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)