dblayer : Add test of foreign keys before INSERT or UPDATE. Reject with an exception if the foreign key doesn't exists
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1582 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
75
dblayer.php
75
dblayer.php
@@ -171,8 +171,16 @@ class dblayer extends PDO
|
||||
}
|
||||
|
||||
/** Create a new entry in the table. Datas must be an indexed array
|
||||
@param array $datas Datas to be recorded (column=>value)*/
|
||||
@param array $datas Datas to be recorded (column=>value)
|
||||
@obsolete 0.5 */
|
||||
public function create ($datas)
|
||||
{
|
||||
return $this->insert ($datas);
|
||||
}
|
||||
|
||||
/** Insert a new line of datas in the table. Datas must be an indexed array
|
||||
@param array $datas Datas to be recorded (column=>value)*/
|
||||
public function insert ($datas)
|
||||
{
|
||||
if ($this->db === null)
|
||||
throw new Exception (_("Database not connected"), 500);
|
||||
@@ -237,8 +245,37 @@ class dblayer extends PDO
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : Check if the foreign keys constrains are valid before doing the
|
||||
// insertion
|
||||
// Check if the foreign keys constrains are valid before doing the insertion
|
||||
foreach ($this->foreign as $foreign=>$data)
|
||||
{
|
||||
$table = $data[0];
|
||||
$column = $data[1];
|
||||
$req = "SELECT $column FROM `$table` WHERE $column=:$column";
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
$st = $this->db->prepare ($req);
|
||||
$val = $datasOK[$foreign];
|
||||
$key = $column;
|
||||
if ($this->debug) echo "DEBUG BIND : $column->".var_export ($val, TRUE).
|
||||
"\n";
|
||||
if ($val === null)
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
|
||||
elseif ($this->fields[$key][0] === "integer")
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_INT);
|
||||
elseif ($this->fields[$key][0] === "varchar")
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||
elseif ($this->fields[$key][0] === "datetime")
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||
else
|
||||
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
||||
$st->execute ();
|
||||
$res = array ();
|
||||
while ($d = $st->fetch (PDO::FETCH_ASSOC))
|
||||
$res[] = $d;
|
||||
if (count ($res) === 0)
|
||||
throw new Exception (sprintf (_("The foreign key '%s' doesn't exists"),
|
||||
$column), 405);
|
||||
}
|
||||
|
||||
$req = "INSERT INTO `".$this->table."` ";
|
||||
$req .= "(".implode (",", array_keys ($datasOK)).")";
|
||||
$req .= " VALUES ";
|
||||
@@ -430,7 +467,37 @@ class dblayer extends PDO
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : Check if the foreign keys constrains are valid before doing the
|
||||
// Check if the foreign keys constrains are valid before doing the update
|
||||
foreach ($this->foreign as $foreign=>$data)
|
||||
{
|
||||
$table = $data[0];
|
||||
$column = $data[1];
|
||||
$req = "SELECT $column FROM `$table` WHERE $column=:$column";
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
$st = $this->db->prepare ($req);
|
||||
$val = $datasOK[$foreign];
|
||||
$key = $column;
|
||||
if ($this->debug) echo "DEBUG BIND : $column->".var_export ($val, TRUE).
|
||||
"\n";
|
||||
if ($val === null)
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
|
||||
elseif ($this->fields[$key][0] === "integer")
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_INT);
|
||||
elseif ($this->fields[$key][0] === "varchar")
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||
elseif ($this->fields[$key][0] === "datetime")
|
||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||
else
|
||||
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
||||
$st->execute ();
|
||||
$res = array ();
|
||||
while ($d = $st->fetch (PDO::FETCH_ASSOC))
|
||||
$res[] = $d;
|
||||
if (count ($res) === 0)
|
||||
throw new Exception (sprintf (_("The foreign key '%s' doesn't exists"),
|
||||
$column), 405);
|
||||
}
|
||||
|
||||
$datasOK[$this->primary] = $updatekey;
|
||||
$req = "UPDATE `".$this->table."` SET ";
|
||||
$i = 0;
|
||||
|
||||
Reference in New Issue
Block a user