PHP - JsonDb A Flat File Database

JsonDb is a simple flat file database library designed to perform CRUD operations on a flat file. In this article we take a look at how JsonDb can be used to store data in a json file. If you need to develop an application where storing data to a flat file is required and you need to be able to query the data with basic search parameters consider the JsonDb PHP library. The JsonDb PHP library makes it very easy to insert/update/delete and query data that is stored in a text file. Take a look at the following code samples for a better understanding.
  1. $db = new JsonDb("path to save document");  
  2. // or   
  3. $db = new JsonDb()
The JsonDb constructor has an optional argument which lets you specify the location to where data files will be stored. The default behavior is to save data files in the same directory as the currently executing script.
  1. $comment = new stdClass();  
  2. $comment->first_name = "John";  
  3. $comment->last_name = "Smith";  
  4. $comment->comment = "This is my first comment";  
  5. $comment->date = date("Y-M-d H:i:s");  
  6.  
  7. $db->comments->insert($comment);  
Objects are inserted to the $db instance by invoking a dynamic property of the $db object. This dynamic property name is used to create the data file. When a new object is inserted, it is given a new dynamic property named __object_id. The __object_id property contains a unique value, which can be used to update/delete and query the inserted object. The example below shows you how to update an object that was previously persisted and given an __object_id.
  1. $objId = "14138983955446609b776c0";  
  2.  
  3. $comment = new stdClass();  
  4. $comment->first_name = "John";  
  5. $comment->last_name = "Smith";  
  6. $comment->comment = "Replace the current docuemnt.";  
  7. $comment->date = date("Y-M-d H:i:s");  
  8.  
  9. $db->comments->update($objId, $comment);  
An object can also be deleted using the __object_id.
  1. $objId = "14138983955446609b776c0";  
  2. $db->comments->remove($objId);  
JsonDb also makes it possible to query data. It has two simples methods that allow you to return filtered data based on search conditions.
  1. $objId = "14138983955446609b776c0";  
  2. $jsonQuery = $db->comments->find($objId);  
  3. //  
  4. $jsonQuery = $db->comments->find(array("first_name" => "John"));  
  5. // or  
  6. $jsonQuery = $db->comments->find(array("first_name" => "John""last_name" => "Smith"))
The find method accepts an array of search conditions or a string as its argument. You can use multiple search conditions in an array. Each search condition performs an "AND" operation on the query. You can also search using an __object_id. The find() method returns an object of type JsonQuery. If results have been found; call either fetchOne() or fetch() on the $jsonQuery object.
  1. $comment = $jsonQuery->fetchOne(); // Returns a single comment  
  2. $comments = $jsonQuery->fetch(); // Returns an array of comments 
You can limit the search results by using the limit() method. The limit() method takes 2 arguments ($start, $count).
  1. $results = $jsonQuery->limit(2,3)->fetch()
Finally, while the find() method allows you to return objects based on search conditions. It cannot deal with complex searches. The query() method allows you to specify a callback function, which can be used to customize the search results.
  1. $results = $db->comments->query(function($document){  
  2.     if($document->first_name == "John" || $document->first_name == "Suzan"){  
  3.         return $document;  
  4.     }  
  5. }true)
As you can see from the code samples, using the JsonDb PHP library is very easy to use. You can use it in situations where a traditional database may not be required. For example, you might want to log user visits to your website or provide a comment feature to allow your users to leave comments. Download

No comments:

Post a Comment