From d590337cf5b7d0ee6e535474e284359d0ae93b0f Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 13 Mar 2015 10:35:24 +0000 Subject: [PATCH] dblayer : add the possibility to filtering on foreignkeys when reading git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2021 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/dblayer.php b/dblayer.php index f6d3cdb..50822b8 100644 --- a/dblayer.php +++ b/dblayer.php @@ -600,9 +600,10 @@ class dblayer extends PDO $order = array (array ($key, $orientation), ...) $key=>column, $orientation=ASC/DESC @param bool|null $whereOr The WHERE parameters are separated by OR instead - of AND*/ + of AND + @param array|null $foreignSelect Add a filter on foreign keys */ public function read ($select=null, $display=null, $order=null, - $whereOr=false) + $whereOr=false, $foreignSelect=null) { if ($this->debug) echo "== Entering read\n"; if ($this->sep === "") @@ -638,9 +639,12 @@ class dblayer extends PDO $req .= implode ("$this->sep,$this->sep", $display); $req .= "$this->sep "; $req .= "FROM $this->sep$this->tableprefix$this->table$this->sep"; + if ($select !== null || $foreignSelect !== null) + { + $req .= " WHERE ("; + } if ($select !== null) { - $req .= " WHERE "; // TODO Allow a field=>value in plus of array("field","value") foreach ($select as $n=>$s) { @@ -662,6 +666,24 @@ class dblayer extends PDO // TODO : Test for PostgreSQL (Tested for SQLite and MySQL) $req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]); } + $req .=")"; + } + if ($select !== null && $foreignSelect !== null) + $req .= " AND ("; + if ($foreignSelect !== null) + { + // TODO Allow a field=>value in plus of array("field","value") + foreach ($foreignSelect as $n=>$s) + { + if ($n > 0) + { + $req .= " AND"; + } + if (!isset ($s[2])) + $s[2] = "="; + $req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]); + } + $req .=")"; } if ($order !== null) @@ -700,6 +722,15 @@ class dblayer extends PDO $st->bindValue (":".md5 ($s[0]), $s[1]); } } + if ($foreignSelect !== null) + { + foreach ($foreignSelect as $s) + { + if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0]).")->". + var_export ($s[1], TRUE)."\n"; + $st->bindValue (":".md5 ($s[0]), $s[1]); + } + } $rc = $st->execute (); if ($rc === false)