Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit e896c15

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

File tree

5 files changed

+115
-54
lines changed

5 files changed

+115
-54
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55

6+
- 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)
7+
68
## [3.9.2] - 2022-09-01
79

810
### Addded

src/Query/Builder.php

Lines changed: 109 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Query\Builder as BaseBuilder;
88
use Illuminate\Database\Query\Expression;
99
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Carbon;
1011
use Illuminate\Support\Collection;
1112
use Illuminate\Support\LazyCollection;
1213
use Illuminate\Support\Str;
@@ -140,7 +141,7 @@ public function __construct(Connection $connection, Processor $processor)
140141
/**
141142
* Set the projections.
142143
*
143-
* @param array $columns
144+
* @param array $columns
144145
* @return $this
145146
*/
146147
public function project($columns)
@@ -152,8 +153,7 @@ public function project($columns)
152153

153154
/**
154155
* Set the cursor timeout in seconds.
155-
*
156-
* @param int $seconds
156+
* @param int $seconds
157157
* @return $this
158158
*/
159159
public function timeout($seconds)
@@ -166,7 +166,7 @@ public function timeout($seconds)
166166
/**
167167
* Set the cursor hint.
168168
*
169-
* @param mixed $index
169+
* @param mixed $index
170170
* @return $this
171171
*/
172172
public function hint($index)
@@ -217,8 +217,8 @@ public function cursor($columns = [])
217217
/**
218218
* Execute the query as a fresh "select" statement.
219219
*
220-
* @param array $columns
221-
* @param bool $returnLazy
220+
* @param array $columns
221+
* @param bool $returnLazy
222222
* @return array|static[]|Collection|LazyCollection
223223
*/
224224
public function getFresh($columns = [], $returnLazy = false)
@@ -524,10 +524,10 @@ public function orderBy($column, $direction = 'asc')
524524
/**
525525
* Add a "where all" clause to the query.
526526
*
527-
* @param string $column
528-
* @param array $values
529-
* @param string $boolean
530-
* @param bool $not
527+
* @param string $column
528+
* @param array $values
529+
* @param string $boolean
530+
* @param bool $not
531531
* @return $this
532532
*/
533533
public function whereAll($column, array $values, $boolean = 'and', $not = false)
@@ -742,8 +742,8 @@ public function truncate(): bool
742742
/**
743743
* Get an array with the values of a given column.
744744
*
745-
* @param string $column
746-
* @param string $key
745+
* @param string $column
746+
* @param string $key
747747
* @return array
748748
*
749749
* @deprecated
@@ -775,9 +775,9 @@ public function raw($expression = null)
775775
/**
776776
* Append one or more values to an array.
777777
*
778-
* @param mixed $column
779-
* @param mixed $value
780-
* @param bool $unique
778+
* @param mixed $column
779+
* @param mixed $value
780+
* @param bool $unique
781781
* @return int
782782
*/
783783
public function push($column, $value = null, $unique = false)
@@ -802,8 +802,8 @@ public function push($column, $value = null, $unique = false)
802802
/**
803803
* Remove one or more values from an array.
804804
*
805-
* @param mixed $column
806-
* @param mixed $value
805+
* @param mixed $column
806+
* @param mixed $value
807807
* @return int
808808
*/
809809
public function pull($column, $value = null)
@@ -826,7 +826,7 @@ public function pull($column, $value = null)
826826
/**
827827
* Remove one or more fields.
828828
*
829-
* @param mixed $columns
829+
* @param mixed $columns
830830
* @return int
831831
*/
832832
public function drop($columns)
@@ -857,8 +857,8 @@ public function newQuery()
857857
/**
858858
* Perform an update query.
859859
*
860-
* @param array $query
861-
* @param array $options
860+
* @param array $query
861+
* @param array $options
862862
* @return int
863863
*/
864864
protected function performUpdate($query, array $options = [])
@@ -882,7 +882,7 @@ protected function performUpdate($query, array $options = [])
882882
/**
883883
* Convert a key to ObjectID if needed.
884884
*
885-
* @param mixed $id
885+
* @param mixed $id
886886
* @return mixed
887887
*/
888888
public function convertKey($id)
@@ -1016,7 +1016,7 @@ protected function compileWheres(): array
10161016
}
10171017

10181018
/**
1019-
* @param array $where
1019+
* @param array $where
10201020
* @return array
10211021
*/
10221022
protected function compileWhereAll(array $where): array
@@ -1027,7 +1027,7 @@ protected function compileWhereAll(array $where): array
10271027
}
10281028

10291029
/**
1030-
* @param array $where
1030+
* @param array $where
10311031
* @return array
10321032
*/
10331033
protected function compileWhereBasic(array $where): array
@@ -1083,7 +1083,7 @@ protected function compileWhereBasic(array $where): array
10831083
}
10841084

10851085
/**
1086-
* @param array $where
1086+
* @param array $where
10871087
* @return mixed
10881088
*/
10891089
protected function compileWhereNested(array $where): mixed
@@ -1094,7 +1094,7 @@ protected function compileWhereNested(array $where): mixed
10941094
}
10951095

10961096
/**
1097-
* @param array $where
1097+
* @param array $where
10981098
* @return array
10991099
*/
11001100
protected function compileWhereIn(array $where): array
@@ -1105,7 +1105,7 @@ protected function compileWhereIn(array $where): array
11051105
}
11061106

11071107
/**
1108-
* @param array $where
1108+
* @param array $where
11091109
* @return array
11101110
*/
11111111
protected function compileWhereNotIn(array $where): array
@@ -1116,7 +1116,7 @@ protected function compileWhereNotIn(array $where): array
11161116
}
11171117

11181118
/**
1119-
* @param array $where
1119+
* @param array $where
11201120
* @return array
11211121
*/
11221122
protected function compileWhereNull(array $where): array
@@ -1128,7 +1128,7 @@ protected function compileWhereNull(array $where): array
11281128
}
11291129

11301130
/**
1131-
* @param array $where
1131+
* @param array $where
11321132
* @return array
11331133
*/
11341134
protected function compileWhereNotNull(array $where): array
@@ -1140,7 +1140,7 @@ protected function compileWhereNotNull(array $where): array
11401140
}
11411141

11421142
/**
1143-
* @param array $where
1143+
* @param array $where
11441144
* @return array
11451145
*/
11461146
protected function compileWhereBetween(array $where): array
@@ -1173,63 +1173,124 @@ protected function compileWhereBetween(array $where): array
11731173
}
11741174

11751175
/**
1176-
* @param array $where
1176+
* @param array $where
11771177
* @return array
11781178
*/
11791179
protected function compileWhereDate(array $where): array
11801180
{
11811181
extract($where);
11821182

1183-
$where['operator'] = $operator;
1184-
$where['value'] = $value;
1183+
$startOfDay = new UTCDateTime(Carbon::parse($value)->startOfDay());
1184+
$endOfDay = new UTCDateTime(Carbon::parse($value)->endOfDay());
11851185

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

11891224
/**
1190-
* @param array $where
1225+
* @param array $where
11911226
* @return array
11921227
*/
11931228
protected function compileWhereMonth(array $where): array
11941229
{
11951230
extract($where);
11961231

1197-
$where['operator'] = $operator;
1198-
$where['value'] = $value;
1232+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1233+
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
11991234

1200-
return $this->compileWhereBasic($where);
1235+
return [
1236+
'$expr' => [
1237+
$operator => [
1238+
[
1239+
'$month' => '$'.$column
1240+
],
1241+
$value,
1242+
],
1243+
],
1244+
];
12011245
}
12021246

12031247
/**
1204-
* @param array $where
1248+
* @param array $where
12051249
* @return array
12061250
*/
12071251
protected function compileWhereDay(array $where): array
12081252
{
12091253
extract($where);
12101254

1211-
$where['operator'] = $operator;
1212-
$where['value'] = $value;
1255+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
1256+
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
12131257

1214-
return $this->compileWhereBasic($where);
1258+
return [
1259+
'$expr' => [
1260+
$operator => [
1261+
[
1262+
'$dayOfMonth' => '$'.$column
1263+
],
1264+
$value,
1265+
],
1266+
],
1267+
];
12151268
}
12161269

12171270
/**
1218-
* @param array $where
1271+
* @param array $where
12191272
* @return array
12201273
*/
12211274
protected function compileWhereYear(array $where): array
12221275
{
12231276
extract($where);
12241277

1225-
$where['operator'] = $operator;
1226-
$where['value'] = $value;
1278+
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
12271279

1228-
return $this->compileWhereBasic($where);
1280+
return [
1281+
'$expr' => [
1282+
$operator => [
1283+
[
1284+
'$year' => '$'.$column
1285+
],
1286+
$value
1287+
],
1288+
],
1289+
];
12291290
}
12301291

12311292
/**
1232-
* @param array $where
1293+
* @param array $where
12331294
* @return array
12341295
*/
12351296
protected function compileWhereTime(array $where): array
@@ -1243,7 +1304,7 @@ protected function compileWhereTime(array $where): array
12431304
}
12441305

12451306
/**
1246-
* @param array $where
1307+
* @param array $where
12471308
* @return mixed
12481309
*/
12491310
protected function compileWhereRaw(array $where): mixed
@@ -1254,7 +1315,7 @@ protected function compileWhereRaw(array $where): mixed
12541315
/**
12551316
* Set custom options for the query.
12561317
*
1257-
* @param array $options
1318+
* @param array $options
12581319
* @return $this
12591320
*/
12601321
public function options(array $options)

tests/QueryBuilderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ public function testFindWithTimeout()
138138
{
139139
$id = DB::collection('users')->insertGetId(['name' => 'John Doe']);
140140

141-
$subscriber = new class implements CommandSubscriber
142-
{
141+
$subscriber = new class implements CommandSubscriber {
143142
public function commandStarted(CommandStartedEvent $event)
144143
{
145144
if ($event->getCommandName() !== 'find') {

tests/QueryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use Carbon\Carbon;
6+
57
class QueryTest extends TestCase
68
{
79
protected static $started = false;

0 commit comments

Comments
 (0)