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)