Skip to content

Commit 5161f99

Browse files
committed
Fix bugs around ISODate
1 parent 754c1f6 commit 5161f99

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @var string
2020
*/
21-
define('MPG_APP_VERSION', '1.0.3');
21+
define('MPG_APP_VERSION', '1.0.4');
2222

2323
/**
2424
* Development mode?

src/Controllers/CollectionController.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function insertOneAction() : Response {
3636
new \MongoDB\BSON\ObjectId($decodedRequestBody['document']['_id']);
3737
}
3838

39-
foreach ($decodedRequestBody['document'] as &$insertValue) {
39+
array_walk_recursive($decodedRequestBody['document'], function(&$insertValue) {
40+
4041
if ( preg_match(MongoDBHelper::ISO_DATE_TIME_REGEX, $insertValue) ) {
4142
$insertValue = new \MongoDB\BSON\UTCDateTime(new \DateTime($insertValue));
4243
}
43-
}
44+
45+
});
4446

4547
try {
4648

@@ -155,14 +157,20 @@ public function findAction() : Response {
155157
return new JsonResponse(500, ErrorNormalizer::normalize($th, __METHOD__));
156158
}
157159

158-
foreach ($documents as $document) {
159-
foreach ($document as &$documentValue) {
160+
foreach ($documents as &$document) {
161+
162+
$document = $document->jsonSerialize();
163+
164+
array_walk_recursive($document, function(&$documentValue) {
165+
160166
if ( is_a($documentValue, '\MongoDB\BSON\ObjectId') ) {
161167
$documentValue = (string) $documentValue;
162168
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDatetime') ) {
163169
$documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
164170
}
165-
}
171+
172+
});
173+
166174
}
167175

168176
return new JsonResponse(200, $documents);
@@ -234,7 +242,19 @@ public function enumFieldsAction() : Response {
234242
return new JsonResponse(200, []);
235243
}
236244

237-
$array = json_decode(json_encode($documents[0]), JSON_OBJECT_AS_ARRAY);
245+
$document = $documents[0]->jsonSerialize();
246+
247+
array_walk_recursive($document, function(&$documentValue) {
248+
249+
if ( is_a($documentValue, '\MongoDB\BSON\ObjectId') ) {
250+
$documentValue = (string) $documentValue;
251+
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDatetime') ) {
252+
$documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
253+
}
254+
255+
});
256+
257+
$array = json_decode(json_encode($document), JSON_OBJECT_AS_ARRAY);
238258

239259
/**
240260
* Converts multidimensional array to 2D array with dot notation keys.
@@ -253,10 +273,7 @@ public function enumFieldsAction() : Response {
253273

254274
$documentFields = array_unique($result);
255275

256-
// We ignore $oid since it represents a \MongoDB\BSON\ObjectId object.
257-
$fixedDocumentFields = str_replace('_id.$oid', '_id', json_encode($documentFields));
258-
259-
return new JsonResponse(200, json_decode($fixedDocumentFields));
276+
return new JsonResponse(200, $documentFields);
260277

261278
}
262279

static/js/mpg.collection.indexes.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,7 @@ MPG.eventListeners.addCreateIndex = function() {
358358
MPG.helpers.doAjaxRequest(
359359
'POST',
360360
MPG_BASE_URL + '/ajaxCollectionCreateIndex',
361-
function(response) {
362-
363-
var createdIndexName = JSON.parse(response);
364-
window.alert('Index created with name: ' + createdIndexName);
361+
function(_response) {
365362

366363
MPG.reloadCollectionIndexes();
367364

0 commit comments

Comments
 (0)