]> git.mjollnir.org Git - moodle.git/commitdiff
Fixes to insert_record (from Petri) to explicitly use field default
authormoodler <moodler>
Mon, 30 Dec 2002 03:25:48 +0000 (03:25 +0000)
committermoodler <moodler>
Mon, 30 Dec 2002 03:25:48 +0000 (03:25 +0000)
values if we know them from ADOdb, to avoid some problems with PostgreSQL 7.2 onwards

lib/datalib.php

index 223b53f6b41c41aac6c35a7bb1e544034f57957f..ce2ca8c431c12cd1d7e5e382fe3aa295011f71f8 100644 (file)
@@ -406,13 +406,26 @@ function insert_record($table, $dataobject, $returnid=true) {
 
     $data = (array)$dataobject;
 
-    // Pull out data matching these fields
+    // Pull out data from the dataobject that matches the fields in the table.
+    // If fields are missing or empty, then try to set the defaults explicitly
+    // because some databases (eg PostgreSQL) don't always set them properly
     foreach ($columns as $column) {
-        if ($column->name <> "id" && isset($data[$column->name]) ) {
-            $ddd[$column->name] = $data[$column->name];
+        if ($column->name <> "id") {
+            if (isset($data[$column->name])) { 
+                if ($data[$column->name] == "" and isset($column->has_default)) {
+                    $ddd[$column->name] = $column->default_value;
+                } else {
+                    $ddd[$column->name] = $data[$column->name];
+                }
+            } else {
+                if (isset($column->has_default)) {
+                    $ddd[$column->name] = $column->default_value;
+                } 
+            }
         }
     }
 
+
     // Construct SQL queries
     if (! $numddd = count($ddd)) {
         return false;