From 8d3f14a779b1b33501970c41ae8d9d81adfe65dc Mon Sep 17 00:00:00 2001 From: Matt Grubb Date: Mon, 1 Jun 2020 15:15:30 -0400 Subject: [PATCH 1/5] Examples --- examples/todo/index.php | 0 {example => examples/usage}/backup.php | 0 {example => examples/usage}/database/test/users.json | 0 {example => examples/usage}/dev.php | 0 {example => examples/usage}/test.php | 0 src/FilerDB/Core/Libraries/Databases.php | 9 --------- 6 files changed, 9 deletions(-) create mode 100755 examples/todo/index.php rename {example => examples/usage}/backup.php (100%) mode change 100644 => 100755 rename {example => examples/usage}/database/test/users.json (100%) mode change 100644 => 100755 rename {example => examples/usage}/dev.php (100%) mode change 100644 => 100755 rename {example => examples/usage}/test.php (100%) mode change 100644 => 100755 diff --git a/examples/todo/index.php b/examples/todo/index.php new file mode 100755 index 0000000..e69de29 diff --git a/example/backup.php b/examples/usage/backup.php old mode 100644 new mode 100755 similarity index 100% rename from example/backup.php rename to examples/usage/backup.php diff --git a/example/database/test/users.json b/examples/usage/database/test/users.json old mode 100644 new mode 100755 similarity index 100% rename from example/database/test/users.json rename to examples/usage/database/test/users.json diff --git a/example/dev.php b/examples/usage/dev.php old mode 100644 new mode 100755 similarity index 100% rename from example/dev.php rename to examples/usage/dev.php diff --git a/example/test.php b/examples/usage/test.php old mode 100644 new mode 100755 similarity index 100% rename from example/test.php rename to examples/usage/test.php diff --git a/src/FilerDB/Core/Libraries/Databases.php b/src/FilerDB/Core/Libraries/Databases.php index bc68af4..1e160f3 100644 --- a/src/FilerDB/Core/Libraries/Databases.php +++ b/src/FilerDB/Core/Libraries/Databases.php @@ -121,13 +121,4 @@ private function retrieveDatabases() { $this->databases = $result; } - /** - * Builds the path for the database in the file system. - * @return string $path - */ - private function path ($database) { - $path = $this->config->root . DIRECTORY_SEPARATOR . $database . DIRECTORY_SEPARATOR; - return $path; - } - } From c7b6dcba410a40b2ecbdf291854e96f3b3b312b5 Mon Sep 17 00:00:00 2001 From: Matt Grubb Date: Mon, 1 Jun 2020 15:40:19 -0400 Subject: [PATCH 2/5] Fixes issues with deleteion by id. --- examples/todo/database/todo/items.json | 1 + examples/todo/filerdb.php | 28 ++++++++++++++++ examples/todo/index.php | 41 +++++++++++++++++++++++ examples/usage/backup.php | 4 +-- examples/usage/dev.php | 4 +-- examples/usage/test.php | 4 +-- src/FilerDB/Core/Helpers/Document.php | 2 +- src/FilerDB/Core/Libraries/Collection.php | 20 ++++++++--- 8 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 examples/todo/database/todo/items.json create mode 100644 examples/todo/filerdb.php diff --git a/examples/todo/database/todo/items.json b/examples/todo/database/todo/items.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/examples/todo/database/todo/items.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/examples/todo/filerdb.php b/examples/todo/filerdb.php new file mode 100644 index 0000000..22af039 --- /dev/null +++ b/examples/todo/filerdb.php @@ -0,0 +1,28 @@ + __DIR__ . '/database', + + // Optional configurations + 'includeTimestamps' => true, + + // Specify database + 'database' => 'todo', + + // Configs + 'createRootIfNotExist' => true, + 'createDatabaseIfNotExist' => true, + 'createCollectionIfNotExist' => true + ]); + + return $filerdb; +} catch (Exception $e) { + return false; +} diff --git a/examples/todo/index.php b/examples/todo/index.php index e69de29..d419466 100755 --- a/examples/todo/index.php +++ b/examples/todo/index.php @@ -0,0 +1,41 @@ +collection('items')->insert([ + 'title' => $todo + ]); + } +} elseif ($action === 'remove') { + $id = (isset($_GET['id']) ? $_GET['id'] : false); + + if ($id !== false) { + $filerdb->collection('items')->id($id)->delete(); + } +} + +// Retrieve all todo items +$items = $filerdb->collection('items')->all(); + +?> + +

Todo Items

+ +
+ + +
+ + diff --git a/examples/usage/backup.php b/examples/usage/backup.php index a94ea57..164e998 100755 --- a/examples/usage/backup.php +++ b/examples/usage/backup.php @@ -1,7 +1,7 @@ empty() instead. */ - if (count($documentsToDelete) === count($originalDocuments)) { - throw new FilerDBException("Please use ->empty() to delete all records"); - return false; + if (is_array($documentsToDelete) && is_array($originalDocuments)) { + if (count($documentsToDelete) === count($originalDocuments)) { + throw new FilerDBException("Please use ->empty() to delete all records"); + return false; + } } /** * Filter out records that match the documents * to be deleted. */ - foreach ($documentsToDelete as $deleteDoc) { + if (is_array($documentsToDelete)) { + foreach ($documentsToDelete as $deleteDoc) { + foreach ($originalDocuments as $key => $origDoc) { + if ($origDoc->id === $deleteDoc->id) { + unset($originalDocuments[$key]); + } + } + } + } else { foreach ($originalDocuments as $key => $origDoc) { - if ($origDoc->id === $deleteDoc->id) { + if ($origDoc->id === $documentsToDelete->id) { unset($originalDocuments[$key]); } } From b73ce625894e6fd4d10d1c9b0dbc3ac9a403e0e0 Mon Sep 17 00:00:00 2001 From: Matt Grubb Date: Mon, 1 Jun 2020 15:41:26 -0400 Subject: [PATCH 3/5] Update todo example --- examples/todo/index.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/todo/index.php b/examples/todo/index.php index d419466..86fca84 100755 --- a/examples/todo/index.php +++ b/examples/todo/index.php @@ -14,12 +14,19 @@ 'title' => $todo ]); } + + header('Location: index.php'); + exit; + } elseif ($action === 'remove') { $id = (isset($_GET['id']) ? $_GET['id'] : false); if ($id !== false) { $filerdb->collection('items')->id($id)->delete(); } + + header('Location: index.php'); + exit; } // Retrieve all todo items From ea17520216c7ae85db86acbd8182304de059dd78 Mon Sep 17 00:00:00 2001 From: Matt Grubb Date: Mon, 1 Jun 2020 15:44:41 -0400 Subject: [PATCH 4/5] Example updates --- examples/todo/database/todo/items.json | 9 ++++++++- examples/todo/index.php | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/todo/database/todo/items.json b/examples/todo/database/todo/items.json index 0637a08..d295942 100644 --- a/examples/todo/database/todo/items.json +++ b/examples/todo/database/todo/items.json @@ -1 +1,8 @@ -[] \ No newline at end of file +[ + { + "title": "An example", + "id": "5ed559fc3669f", + "createdAt": 1591040508, + "updatedAt": 1591040508 + } +] \ No newline at end of file diff --git a/examples/todo/index.php b/examples/todo/index.php index 86fca84..d8a3798 100755 --- a/examples/todo/index.php +++ b/examples/todo/index.php @@ -3,6 +3,8 @@ // Include a file that returns the filerdb instance. $filerdb = require __DIR__ . '/filerdb.php'; +if (!$filerdb) die('FilerDB was unable to instantiate.'); + // Current action $action = (isset($_GET['a']) ? $_GET['a'] : false); From c7005ea3a3c9783eee69415f75c51309038016c4 Mon Sep 17 00:00:00 2001 From: Matt Grubb Date: Thu, 11 Jun 2020 11:52:33 -0400 Subject: [PATCH 5/5] Update Collection.php --- src/FilerDB/Core/Libraries/Collection.php | 39 +++++++++++------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/FilerDB/Core/Libraries/Collection.php b/src/FilerDB/Core/Libraries/Collection.php index ec48f0c..87f4cf7 100644 --- a/src/FilerDB/Core/Libraries/Collection.php +++ b/src/FilerDB/Core/Libraries/Collection.php @@ -100,7 +100,13 @@ public function __construct ($config = null, $database, $collection) { public function id ($id) { $documents = $this->documents; $data = Document::byId($documents, $id); - if ($data === false) return false; + + if ($data === false) { + $this->documents = false; + $this->response = false; + return $this; + } + $this->documents = $documents[$data->index]; $this->response = $this->documents; return $this; @@ -113,13 +119,17 @@ public function id ($id) { */ public function get ($fields = false) { - /** - * If the columns parameter is provided, - * and is an array. - */ - if (is_array($fields)) { - if (count($fields) >= 1) { - return $this->pickFieldsFromData($this->response, $fields); + // Make sure there is a document(s) to iterate through. + if ($this->response !== false) { + + /** + * If the columns parameter is provided, + * and is an array. + */ + if (is_array($fields)) { + if (count($fields) >= 1) { + return $this->pickFieldsFromData($this->response, $fields); + } } } @@ -489,19 +499,6 @@ public function delete () { $documentsToDelete = $this->documents; $originalDocuments = $this->getDocuments(); - /** - * Fail safe, we do not want to delete all records - * with this method. - * - * Warn them to use ->empty() instead. - */ - if (is_array($documentsToDelete) && is_array($originalDocuments)) { - if (count($documentsToDelete) === count($originalDocuments)) { - throw new FilerDBException("Please use ->empty() to delete all records"); - return false; - } - } - /** * Filter out records that match the documents * to be deleted.