From 383e831ff3adb00297e6c71f37ea569747a68189 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 18 Mar 2014 18:35:48 +0000 Subject: [PATCH] Add support to read in dblayer with order and colmuns displayed Return the number of deleted rows in delete git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1223 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/dblayer.php b/dblayer.php index 7b0ffce..db9765a 100644 --- a/dblayer.php +++ b/dblayer.php @@ -102,17 +102,27 @@ class dblayer extends PDO return $this->db->lastInsertId(); } - /** Read all the table or only one line defined by the select key, the select + /** Read the table content based on a select filter, ordered by order operator and the associated select value - $select = array (array ($key, $val, $operator)) - $key=>column, $val=>value to found, $operator=>'LIKE', =... - $order = array (array ($key, $orientation)) - $key=>column, $orientation=ASC/DESC + - $select = array (array ($key, $val, $operator), ...) + $key=>column, $val=>value to found, $operator=>'LIKE', =... + - $display = array ($col1, $col2...); + Columns displayed + - $order = array (array ($key, $orientation), ...) + $key=>column, $orientation=ASC/DESC */ - function read ($select=null, $order=null) + function read ($select=null, $display=null, $order=null) { if ($this->db === null) throw new Exception ("Database not connected"); + if ($display !== null) + { + foreach ($display as $f) + { + if (!in_array ($f, array_keys ($this->fields))) + throw new Exception ("Field $f not allowed"); + } + } $req = "SELECT "; $req .= implode (",", array_keys ($this->fields)); $req .= " FROM `".$this->table."`"; @@ -155,6 +165,7 @@ class dblayer extends PDO $st->bindValue (":".$s[0], $s[1]); } } + $st->execute (); $res = array (); while ($d = $st->fetch (PDO::FETCH_ASSOC)) @@ -184,6 +195,7 @@ class dblayer extends PDO $req .= "$key=:$key"; $i++; } + $req .= " WHERE $this->primary=:primary"; if ($this->debug) echo "DEBUG : $req\n"; $st = $this->db->prepare ($req); @@ -208,7 +220,8 @@ class dblayer extends PDO $st->execute (); } - /** Delete a tuple identified by its primary key */ + /** Delete a tuple identified by its primary key + Return the number of deleted rows (can be 0 !) */ function delete ($deletekey) { if ($this->db === null) @@ -221,6 +234,7 @@ class dblayer extends PDO var_export ($deletekey, TRUE)."\n"; $st->bindValue (":primary", $deletekey); $st->execute (); + return $st->rowCount(); } }