Skip to content

Commit

Permalink
Fix bugs around ISODate
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Aug 14, 2020
1 parent 754c1f6 commit 5161f99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @var string
*/
define('MPG_APP_VERSION', '1.0.3');
define('MPG_APP_VERSION', '1.0.4');

/**
* Development mode?
Expand Down
37 changes: 27 additions & 10 deletions src/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public function insertOneAction() : Response {
new \MongoDB\BSON\ObjectId($decodedRequestBody['document']['_id']);
}

foreach ($decodedRequestBody['document'] as &$insertValue) {
array_walk_recursive($decodedRequestBody['document'], function(&$insertValue) {

if ( preg_match(MongoDBHelper::ISO_DATE_TIME_REGEX, $insertValue) ) {
$insertValue = new \MongoDB\BSON\UTCDateTime(new \DateTime($insertValue));
}
}

});

try {

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

foreach ($documents as $document) {
foreach ($document as &$documentValue) {
foreach ($documents as &$document) {

$document = $document->jsonSerialize();

array_walk_recursive($document, function(&$documentValue) {

if ( is_a($documentValue, '\MongoDB\BSON\ObjectId') ) {
$documentValue = (string) $documentValue;
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDatetime') ) {
$documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
}
}

});

}

return new JsonResponse(200, $documents);
Expand Down Expand Up @@ -234,7 +242,19 @@ public function enumFieldsAction() : Response {
return new JsonResponse(200, []);
}

$array = json_decode(json_encode($documents[0]), JSON_OBJECT_AS_ARRAY);
$document = $documents[0]->jsonSerialize();

array_walk_recursive($document, function(&$documentValue) {

if ( is_a($documentValue, '\MongoDB\BSON\ObjectId') ) {
$documentValue = (string) $documentValue;
} elseif ( is_a($documentValue, '\MongoDB\BSON\UTCDatetime') ) {
$documentValue = $documentValue->toDateTime()->format('Y-m-d\TH:i:s.v\Z');
}

});

$array = json_decode(json_encode($document), JSON_OBJECT_AS_ARRAY);

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

$documentFields = array_unique($result);

// We ignore $oid since it represents a \MongoDB\BSON\ObjectId object.
$fixedDocumentFields = str_replace('_id.$oid', '_id', json_encode($documentFields));

return new JsonResponse(200, json_decode($fixedDocumentFields));
return new JsonResponse(200, $documentFields);

}

Expand Down
5 changes: 1 addition & 4 deletions static/js/mpg.collection.indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ MPG.eventListeners.addCreateIndex = function() {
MPG.helpers.doAjaxRequest(
'POST',
MPG_BASE_URL + '/ajaxCollectionCreateIndex',
function(response) {

var createdIndexName = JSON.parse(response);
window.alert('Index created with name: ' + createdIndexName);
function(_response) {

MPG.reloadCollectionIndexes();

Expand Down

0 comments on commit 5161f99

Please sign in to comment.