From 0cef265a61baacdcf894a243402b59edf4ab393d Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 21 Aug 2023 14:52:10 -0400 Subject: [PATCH 1/2] Fix internal keys not removed when was is null --- src/Database/Adapter/MariaDB.php | 34 +++++++++++++++---------------- src/Database/Adapter/Postgres.php | 34 +++++++++++++++---------------- src/Database/Adapter/SQL.php | 10 ++++----- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 040e2571c..a3830e40a 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -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) { diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 6a7642e28..96435d574 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -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) { diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 50de353ef..d4e9d9320 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -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']); } From 9eca9b53890fa37ddaebea9cc00452835b0edfca Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 21 Aug 2023 15:00:33 -0400 Subject: [PATCH 2/2] Merge branch 'fix-no-invalid-keys' of github.com:utopia-php/database into fix-no-invalid-keys --- src/Database/Database.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 48c2968b9..4a5ec263d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -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));