From d7630c4c7ff9feaae5cbfd4f2a5d862188964db0 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 01:46:19 +0330 Subject: [PATCH 1/6] fix bug #41 "results() returns document instead of array #41" --- src/QueryLogic.php | 40 ++++++++++++++++++++++++---------------- tests/DatabaseTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/QueryLogic.php b/src/QueryLogic.php index 53e1c38..e573d86 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -42,6 +42,29 @@ public function __construct(Database $database) } } + private function loadDocuments() + { + $predicates = $this->predicate->get(); + + if ($this->cache===false) + { + $this->documents = $this->database->findAll(true,false); + return $this; + } + + $this->cache->setKey(json_encode($predicates)); + + if ($cached_documents = $this->cache->get()) + { + $this->documents = $cached_documents; + + $this->sort(); + $this->offsetLimit(); + return $this; + } + $this->documents = $this->database->findAll(true,false); + return $this; + } /** * run * @@ -57,22 +80,7 @@ public function run() $predicates = 'findAll'; } - if ($this->cache !== false) - { - $this->cache->setKey(json_encode($predicates)); - - if ($cached_documents = $this->cache->get()) - { - $this->documents = $cached_documents; - - $this->sort(); - $this->offsetLimit(); - - return $this; - } - } - - $this->documents = $this->database->findAll(true,false); + $this->loadDocuments(); if ($predicates !== 'findAll') { diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 0febba8..e187e71 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -308,4 +308,34 @@ public function test_must_return_exception_on_non_exist_method() $this->expectException(\BadMethodCallException::class); $results = $db->none('name','=','John')->andWhere('email','==','john@example.com')->resultDocuments(); } + /** + * based on issue #41 + * results() returns document instead of array #41 + */ + public function test_must_return_array_on_select_an_culomn_from_cache() + { + $db = new \Filebase\Database([ + 'dir' => __DIR__.'/databases/saved', + 'cache' => true + ]); + + $db->flush(true); + + for ($x = 1; $x <= 10; $x++) + { + $user = $db->get(uniqid()); + $user->name = 'John'; + $user->email = 'john@example.com'; + $user->save(); + } + + $db->where('name','=','John')->andWhere('email','==','john@example.com')->select('email')->results(); + $result_from_cache = $db->where('name','=','John')->andWhere('email','==','john@example.com')->select('email')->results(); + + $this->assertCount(10,$result_from_cache); + $this->assertEquals(['email'=>'john@example.com'],$result_from_cache[0]); + $this->assertInternalType('array', $result_from_cache[0]); + $this->assertInternalType('string', $result_from_cache[0]['email']); + $db->flush(true); + } } From b660650472362e45f78d97cb395f09889196d164 Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Sun, 24 Feb 2019 17:55:23 -0500 Subject: [PATCH 2/6] Updated changelog and minor comment block updates. --- CHANGELOG.md | 3 +++ src/Database.php | 2 +- src/QueryLogic.php | 7 ++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 026e4b2..856a4e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Change Log ========== +### 02/24/2019 - 1.0.24 +* Merged [Pull Request](https://github.com/filebase/Filebase/pull/50) Fixed [bug](https://github.com/filebase/Filebase/issues/41) returning unexpected results. + ### 02/24/2019 - 1.0.23 * Merged [Pull Request](https://github.com/filebase/Filebase/pull/49) Added support for order by multiple columns * Merged [Pull Request](https://github.com/filebase/Filebase/pull/46) Added ability to query document ids (internal id) diff --git a/src/Database.php b/src/Database.php index 559b8cb..40b189b 100644 --- a/src/Database.php +++ b/src/Database.php @@ -15,7 +15,7 @@ class Database * Stores the version of Filebase * use $db->getVersion() */ - const VERSION = '1.0.23'; + const VERSION = '1.0.24'; /** * $config diff --git a/src/QueryLogic.php b/src/QueryLogic.php index e573d86..0d97c4f 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -42,6 +42,10 @@ public function __construct(Database $database) } } + /** + * loadDocuments + * + */ private function loadDocuments() { $predicates = $this->predicate->get(); @@ -60,11 +64,12 @@ private function loadDocuments() $this->sort(); $this->offsetLimit(); - return $this; + return $this; } $this->documents = $this->database->findAll(true,false); return $this; } + /** * run * From efe82dc49069a4f67603c27d50682ad160b5fdd3 Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Wed, 26 Jun 2019 15:02:39 -0400 Subject: [PATCH 3/6] Updated composer (minor change) --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 13a3e3b..fd12390 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { "description": "A Simple but Powerful Flat File Database Storage.", "keywords": ["filebase", "key-value","file-files", "flat", "file", "database", "document", "flat-file", "serverless"], - "name": "filebase/filebase", - "homepage": "https://github.com/filebase/Filebase", + "name": "tmarois/filebase", + "homepage": "https://github.com/tmarois/Filebase", "type": "package", "licence": "MIT", From 6f436e3eb40a0f4702d5544f7f1bf64a07b4c238 Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Wed, 26 Jun 2019 15:07:12 -0400 Subject: [PATCH 4/6] Updated readme [ci skip] --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index fdaede9..e82720e 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Filebase -[![Build Status](https://travis-ci.org/filebase/Filebase.svg?branch=1.0)](https://travis-ci.org/filebase/Filebase) [![Coverage Status](https://coveralls.io/repos/github/filebase/Filebase/badge.svg?branch=1.0)](https://coveralls.io/github/filebase/Filebase?branch=1.0) [![Slack](http://timothymarois.com/a/slack-02.svg)](https://join.slack.com/t/basephp/shared_invite/enQtNDI0MzQyMDE0MDAwLWU3Nzg0Yjk4MjM0OWVmZDZjMjEyYWE2YjA1ODFhNjI2MzI3MjAyOTIyOTRkMmVlNWNhZWYzMTIwZDJlOWQ2ZTA) +[![Build Status](https://travis-ci.org/tmarois/Filebase.svg?branch=1.0)](https://travis-ci.org/tmarois/Filebase) [![Coverage Status](https://coveralls.io/repos/github/tmarois/Filebase/badge.svg?branch=1.0)](https://coveralls.io/github/tmarois/Filebase?branch=1.0) [![Slack](http://timothymarois.com/a/slack-02.svg)](https://join.slack.com/t/basephp/shared_invite/enQtNDI0MzQyMDE0MDAwLWU3Nzg0Yjk4MjM0OWVmZDZjMjEyYWE2YjA1ODFhNjI2MzI3MjAyOTIyOTRkMmVlNWNhZWYzMTIwZDJlOWQ2ZTA) -A Simple but Powerful Flat File Database Storage. No need for MySQL or an expensive SQL server, in fact, you just need your current site or application setup. All database entries are stored in files ([formatted](https://github.com/filebase/Filebase#2-formatting) the way you like). +A Simple but Powerful Flat File Database Storage. No need for MySQL or an expensive SQL server, in fact, you just need your current site or application setup. All database entries are stored in files ([formatted](README.md#2-formatting) the way you like). You can even modify the raw data within the files themselves without ever needing to use the API. And even better you can put all your files in **version control** and pass them to your team without having out-of-sync SQL databases. @@ -17,12 +17,12 @@ Works with **PHP 5.6** and **PHP 7+** Filebase is simple by design, but has enough features for the more advanced. * Key/Value and Array-based Data Storing -* [Querying data](https://github.com/filebase/Filebase#8-queries) -* [Custom filters](https://github.com/filebase/Filebase#7-custom-filters) -* [Caching](https://github.com/filebase/Filebase#9-caching) (queries) -* [Database Backups](https://github.com/filebase/Filebase#10-database-backups) -* [Formatting](https://github.com/filebase/Filebase#2-formatting) (encode/decode) -* [Validation](https://github.com/filebase/Filebase#6-validation-optional) (on save) +* [Querying data](README.md#8-queries) +* [Custom filters](README.md#7-custom-filters) +* [Caching](README.md#9-caching) (queries) +* [Database Backups](README.md#10-database-backups) +* [Formatting](README.md#2-formatting) (encode/decode) +* [Validation](README.md#6-validation-optional) (on save) * CRUD (method APIs) * File locking (on save) * Intuitive Method Naming @@ -106,8 +106,8 @@ $db = new \Filebase\Database([ |`dir` |string |current directory |The directory where the database files are stored. | |`backupLocation` |string |current directory (`/backups`) |The directory where the backup zip files will be stored. | |`format` |object |`\Filebase\Format\Json` |The format class used to encode/decode data | -|`validate` |array | |Check [Validation Rules](https://github.com/filebase/Filebase#6-validation-optional) for more details | -|`cache` |bool |true |Stores [query](https://github.com/filebase/Filebase#8-queries) results into cache for faster loading. | +|`validate` |array | |Check [Validation Rules](README.md#6-validation-optional) for more details | +|`cache` |bool |true |Stores [query](README.md#8-queries) results into cache for faster loading. | |`cache_expire` |int |1800 |How long caching will last (in seconds) | |`pretty` |bool |true |Store the data for human readability? Pretty Print | |`safe_filename` |bool |true |Automatically converts the file name to a valid name (added: 1.0.13) | @@ -157,7 +157,7 @@ $item = $db->get($userId); |`updatedAt()` | Document was updated (default Y-m-d H:i:s) | |`field()` | You can also use `.` dot delimiter to find values from nested arrays | |`isCache()` | (true/false) if the current document is loaded from cache | -|`filter()` | Refer to the [Custom Filters](https://github.com/filebase/Filebase#7-custom-filters) | +|`filter()` | Refer to the [Custom Filters](README.md#7-custom-filters) | Example: @@ -219,15 +219,15 @@ Here is a list of methods you can use on the database class. |Method|Details| |---|---| |`version()` | Current version of your Filebase library | -|`get($id)` | Refer to [get()](https://github.com/filebase/Filebase#3-get-and-methods) | +|`get($id)` | Refer to [get()](README.md#3-get-and-methods) | |`has($id)` | Check if a record exist returning true/false | |`findAll()` | Returns all documents in database | |`count()` | Number of documents in database | |`flush(true)` | Deletes all documents. | |`flushCache()` | Clears all the cache | |`truncate()` | Deletes all documents. Alias of `flush(true)` | -|`query()` | Refer to the [Queries](https://github.com/filebase/Filebase#8-queries) | -|`backup()` | Refer to the [Backups](https://github.com/filebase/Filebase#10-database-backups) | +|`query()` | Refer to the [Queries](README.md#8-queries) | +|`backup()` | Refer to the [Backups](README.md#10-database-backups) | Examples From 2801db9d705ef10afb12fd4f27cca36ff8865aec Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Wed, 26 Jun 2019 15:23:49 -0400 Subject: [PATCH 5/6] Update readme for composer require [ci skip] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e82720e..a87496e 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Filebase is simple by design, but has enough features for the more advanced. Use [Composer](http://getcomposer.org/) to install package. -Run `composer require filebase/filebase:^1.0` +Run `composer require tmarois/filebase:^1.0` If you do not want to use composer, download the files, and include it within your application, it does not have any dependencies, you will just need to keep it updated with any future releases. From f369cfb0e5727552f717999f719c8643a686484e Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Wed, 24 Feb 2021 13:39:19 -0500 Subject: [PATCH 6/6] Updated readme with discord invite link --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a87496e..41c324c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Filebase -[![Build Status](https://travis-ci.org/tmarois/Filebase.svg?branch=1.0)](https://travis-ci.org/tmarois/Filebase) [![Coverage Status](https://coveralls.io/repos/github/tmarois/Filebase/badge.svg?branch=1.0)](https://coveralls.io/github/tmarois/Filebase?branch=1.0) [![Slack](http://timothymarois.com/a/slack-02.svg)](https://join.slack.com/t/basephp/shared_invite/enQtNDI0MzQyMDE0MDAwLWU3Nzg0Yjk4MjM0OWVmZDZjMjEyYWE2YjA1ODFhNjI2MzI3MjAyOTIyOTRkMmVlNWNhZWYzMTIwZDJlOWQ2ZTA) +[![Build Status](https://travis-ci.org/tmarois/Filebase.svg?branch=1.0)](https://travis-ci.org/tmarois/Filebase) [![Coverage Status](https://coveralls.io/repos/github/tmarois/Filebase/badge.svg?branch=1.0)](https://coveralls.io/github/tmarois/Filebase?branch=1.0) + +[Join Discord](https://discord.gg/kywDsDnJ6C) – For support, updates and collaboration. A Simple but Powerful Flat File Database Storage. No need for MySQL or an expensive SQL server, in fact, you just need your current site or application setup. All database entries are stored in files ([formatted](README.md#2-formatting) the way you like).