Parameters for read in dblayer are provided in array to have more than one criteria.

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1220 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-03-17 12:52:57 +00:00
parent e6309dc7a4
commit 0a8051a1ce

View File

@@ -103,29 +103,40 @@ class dblayer extends PDO
} }
/** Read all the table or only one line defined by the select key, the select /** Read all the table or only one line defined by the select key, the select
operator and the associated select value */ operator and the associated select value
function read ($selectkey=null, $selectval=null, $selectop="=") $select = array (array ($key, $val, $operator))
$key=>column, $val=>value to found, $operator=>'LIKE', =...
*/
function read ($select=null)
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception ("Database not connected");
if ($selectop !== "=" && $selectop !== "like" && $selectop !== null)
throw new Exception ("Invalid selectop");
if ($selectkey !== null &&
! in_array ($selectkey, array_keys ($this->fields)))
throw new Exception ("Unknown key field provided");
$req = "SELECT "; $req = "SELECT ";
$req .= implode (",", array_keys ($this->fields)); $req .= implode (",", array_keys ($this->fields));
$req .= " FROM `".$this->table."`"; $req .= " FROM `".$this->table."`";
if ($selectkey !== NULL && $selectval !== NULL) if ($select !== null)
$req .= " WHERE $selectkey $selectop :selectkey"; {
$req .= " WHERE ";
foreach ($select as $n=>$s)
{
if ($n > 0)
$req .= " AND ";
if (!isset ($s[2]))
$s[2] = "=";
$req .= " ".$s[0]." ".$s[2]." :".$s[0];
}
}
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req); $st = $this->db->prepare ($req);
if ($selectkey !== NULL && $selectval !== NULL) if ($select !== NULL)
{ {
if ($this->debug) echo "DEBUG BIND : selectkey->". foreach ($select as $s)
var_export ($selectkey, TRUE)."\n"; {
$st->bindValue (":selectkey", $selectval); if ($this->debug) echo "DEBUG BIND : ".$s[0]."->".
var_export ($s[1], TRUE)."\n";
$st->bindValue (":".$s[0], $s[1]);
}
} }
$st->execute (); $st->execute ();
$res = array (); $res = array ();