From 09c859ee988047302281f35ff1f6c2f18fc8ea36 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sat, 27 Feb 2016 16:54:46 +0000 Subject: [PATCH] dbjson : if the database is empty, allow to read it without error git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2583 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dbjson.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dbjson.php b/dbjson.php index 731c790..68a1380 100644 --- a/dbjson.php +++ b/dbjson.php @@ -275,6 +275,8 @@ class dbjson if ($this->db === null) $this->db = $this->readDB (); $keys = array (); + if (! array_key_exists ($collection, $this->db)) + $this->db[$collection]["content"] = array (); foreach ($this->db[$collection]["content"] as $key=>$document) { if ($filter === array ()) @@ -384,7 +386,10 @@ class dbjson */ private function readDB () { - return json_decode (file_get_contents ($this->dbfile), true); + $res = json_decode (file_get_contents ($this->dbfile), true); + if ($res === null) + $res = array (); + return $res; } /** Write the dbfile with the provided data. This function don't do locks !