Skip to content

Commit

Permalink
Merge pull request #311 from utopia-php/fix-no-invalid-keys
Browse files Browse the repository at this point in the history
Fix no invalid keys
  • Loading branch information
abnegate authored Aug 21, 2023
2 parents bc5ceb3 + 9eca9b5 commit e0ec678
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
34 changes: 17 additions & 17 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,29 +1085,29 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

$results = $stmt->fetchAll();

foreach ($results as $key => $value) {
if (isset($value['_uid'])) {
$results[$key]['$id'] = $value['_uid'];
unset($results[$key]['_uid']);
foreach ($results as $index => $document) {
if (\array_key_exists('_uid', $document)) {
$results[$index]['$id'] = $document['_uid'];
unset($results[$index]['_uid']);
}
if (isset($value['_id'])) {
$results[$key]['$internalId'] = $value['_id'];
unset($results[$key]['_id']);
if (\array_key_exists('_id', $document)) {
$results[$index]['$internalId'] = $document['_id'];
unset($results[$index]['_id']);
}
if (isset($value['_createdAt'])) {
$results[$key]['$createdAt'] = $value['_createdAt'];
unset($results[$key]['_createdAt']);
if (\array_key_exists('_createdAt', $document)) {
$results[$index]['$createdAt'] = $document['_createdAt'];
unset($results[$index]['_createdAt']);
}
if (isset($value['_updatedAt'])) {
$results[$key]['$updatedAt'] = $value['_updatedAt'];
unset($results[$key]['_updatedAt']);
if (\array_key_exists('_updatedAt', $document)) {
$results[$index]['$updatedAt'] = $document['_updatedAt'];
unset($results[$index]['_updatedAt']);
}
if (isset($value['_permissions'])) {
$results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true);
unset($results[$key]['_permissions']);
if (\array_key_exists('_permissions', $document)) {
$results[$index]['$permissions'] = \json_decode($document['_permissions'] ?? '[]', true);
unset($results[$index]['_permissions']);
}

$results[$key] = new Document($results[$key]);
$results[$index] = new Document($results[$index]);
}

if ($cursorDirection === Database::CURSOR_BEFORE) {
Expand Down
34 changes: 17 additions & 17 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,29 +1090,29 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

$results = $stmt->fetchAll();

foreach ($results as $key => $value) {
if (isset($value['_uid'])) {
$results[$key]['$id'] = $value['_uid'];
unset($results[$key]['_uid']);
foreach ($results as $index => $document) {
if (\array_key_exists('_uid', $document)) {
$results[$index]['$id'] = $document['_uid'];
unset($results[$index]['_uid']);
}
if (isset($value['_id'])) {
$results[$key]['$internalId'] = $value['_id'];
unset($results[$key]['_id']);
if (\array_key_exists('_id', $document)) {
$results[$index]['$internalId'] = $document['_id'];
unset($results[$index]['_id']);
}
if (isset($value['_createdAt'])) {
$results[$key]['$createdAt'] = $value['_createdAt'];
unset($results[$key]['_createdAt']);
if (\array_key_exists('_createdAt', $document)) {
$results[$index]['$createdAt'] = $document['_createdAt'];
unset($results[$index]['_createdAt']);
}
if (isset($value['_updatedAt'])) {
$results[$key]['$updatedAt'] = $value['_updatedAt'];
unset($results[$key]['_updatedAt']);
if (\array_key_exists('_updatedAt', $document)) {
$results[$index]['$updatedAt'] = $document['_updatedAt'];
unset($results[$index]['_updatedAt']);
}
if (isset($value['_permissions'])) {
$results[$key]['$permissions'] = json_decode($value['_permissions'] ?? '[]', true);
unset($results[$key]['_permissions']);
if (\array_key_exists('_permissions', $document)) {
$results[$index]['$permissions'] = \json_decode($document['_permissions'] ?? '[]', true);
unset($results[$index]['_permissions']);
}

$results[$key] = new Document($results[$key]);
$results[$index] = new Document($results[$index]);
}

if ($cursorDirection === Database::CURSOR_BEFORE) {
Expand Down
10 changes: 5 additions & 5 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,23 @@ public function getDocument(string $collection, string $id, array $queries = [])
return new Document([]);
}

if (isset($document['_id'])) {
if (\array_key_exists('_id', $document)) {
$document['$internalId'] = $document['_id'];
unset($document['_id']);
}
if (isset($document['_uid'])) {
if (\array_key_exists('_uid', $document)) {
$document['$id'] = $document['_uid'];
unset($document['_uid']);
}
if (isset($document['_createdAt'])) {
if (\array_key_exists('_createdAt', $document)) {
$document['$createdAt'] = $document['_createdAt'];
unset($document['_createdAt']);
}
if (isset($document['_updatedAt'])) {
if (\array_key_exists('_updatedAt', $document)) {
$document['$updatedAt'] = $document['_updatedAt'];
unset($document['_updatedAt']);
}
if (isset($document['_permissions'])) {
if (\array_key_exists('_permissions', $document)) {
$document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true);
unset($document['_permissions']);
}
Expand Down
1 change: 0 additions & 1 deletion src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2923,7 +2923,6 @@ public function updateDocument(string $collection, string $id, Document $documen
$document = \array_merge($old->getArrayCopy(), $document->getArrayCopy());
$document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collectionID
$document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt
$document['$id'] = $old->getId();
$document = new Document($document);

$collection = $this->silent(fn () => $this->getCollection($collection));
Expand Down

0 comments on commit e0ec678

Please sign in to comment.