From 81e298a0896a735c99e34b2fc17d489ee329bf9e Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 12 Jun 2014 14:39:44 +0000 Subject: [PATCH] The createTable must use the class fields and not external variables. Easier ! git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1443 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/dblayer.php b/dblayer.php index 3494861..af6e38b 100644 --- a/dblayer.php +++ b/dblayer.php @@ -39,6 +39,8 @@ class dblayer extends PDO protected $primary = null; /** An array to define the unique fields (or array of unique fields) */ protected $unique = null; + /** An array to define the foreign keys of the field */ + protected $foreign = array (); /** The db connection */ protected $db = null; /** The verify stack */ @@ -461,17 +463,16 @@ class dblayer extends PDO $unique = array ("id", array ("zo ne", "vie wname")); $foreign = array ("zone"=>"table.field",...); */ - public function createTable ($table, $fields, $primary, $unique, - $foreign = array ()) + public function createTable () { if ($this->db === null) throw new Exception ("Database not connected"); switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) { case "sqlite": - $sql = "CREATE TABLE `$table` (\n"; + $sql = "CREATE TABLE `$this->table` (\n"; $i = 0; - foreach ($fields as $field=>$params) + foreach ($this->fields as $field=>$params) { if ($i > 0) $sql .= ",\n"; @@ -500,7 +501,7 @@ class dblayer extends PDO throw new Exception (_("Unknown type provided for field")); } // Primary key - if ($primary === $field) + if ($this->primary === $field) $sql .= " PRIMARY KEY"; // Others parameters for field // Sort to put the autoincrement field in front of params, if it is @@ -520,9 +521,9 @@ class dblayer extends PDO $i ++; } // Unique fields - if ($unique !== null) + if ($this->unique !== null) { - foreach ($unique as $u) + foreach ($this->unique as $u) { $sql .= ",\n UNIQUE (`"; if (is_array ($u)) @@ -534,7 +535,7 @@ class dblayer extends PDO } // Foreign keys $i = 0; - foreach ($foreign as $field=>$k) + foreach ($this->foreign as $field=>$k) { $sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`". $k[1]."`)"; @@ -547,9 +548,9 @@ class dblayer extends PDO $sql .=")"; break; case "mysql": - $sql = "CREATE TABLE `$table` (\n"; + $sql = "CREATE TABLE `$this->table` (\n"; $i = 0; - foreach ($fields as $field=>$params) + foreach ($this->fields as $field=>$params) { if ($i > 0) $sql .= ",\n"; @@ -578,7 +579,7 @@ class dblayer extends PDO throw new Exception (_("Unknown type provided for field")); } // Primary key - if ($primary === $field) + if ($this->primary === $field) $sql .= " PRIMARY KEY"; // Others parameters for field // Sort to put the autoincrement field in front of params, if it is @@ -598,9 +599,9 @@ class dblayer extends PDO $i ++; } // Unique fields - if ($unique !== null) + if ($this->unique !== null) { - foreach ($unique as $u) + foreach ($this->unique as $u) { $sql .= ",\n UNIQUE (`"; if (is_array ($u)) @@ -612,7 +613,7 @@ class dblayer extends PDO } // Foreign keys $i = 0; - foreach ($foreign as $field=>$k) + foreach ($this->foreign as $field=>$k) { $sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`". @@ -626,9 +627,9 @@ class dblayer extends PDO $sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; break; case "pgsql": - $sql = "CREATE TABLE \"$table\" (\n"; + $sql = "CREATE TABLE \"$this->table\" (\n"; $i = 0; - foreach ($fields as $field=>$params) + foreach ($this->fields as $field=>$params) { if ($i > 0) $sql .= ",\n"; @@ -661,7 +662,7 @@ class dblayer extends PDO throw new Exception (_("Unknown type provided for field")); } // Primary key - if ($primary === $field) + if ($this->primary === $field) $sql .= " PRIMARY KEY"; // Others parameters for field // Sort to put the autoincrement field in front of params, if it is @@ -681,9 +682,9 @@ class dblayer extends PDO $i ++; } // Unique fields - if ($unique !== null) + if ($this->unique !== null) { - foreach ($unique as $u) + foreach ($this->unique as $u) { $sql .= ",\n UNIQUE (\""; if (is_array ($u)) @@ -695,7 +696,7 @@ class dblayer extends PDO } // Foreign keys $i = 0; - foreach ($foreign as $field=>$k) + foreach ($this->foreign as $field=>$k) { $sql .= ",\n FOREIGN KEY(\"$field\") REFERENCES \"".$k[0]."\"(\"". $k[1]."\")"; @@ -711,7 +712,9 @@ class dblayer extends PDO throw new Exception (_("PDO Engine not supported in dbLayer", 500)); } - echo "$sql\n"; + if ($this->debug) + echo "$sql\n"; + return $this->db->exec($sql); } }