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
This commit is contained in:
2015-03-13 10:35:24 +00:00
parent ce1ecca5da
commit d590337cf5

View File

@@ -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)