From 8f3eaed07b9885d2b98df3ed60c7b659b17fc5dc Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 4 Apr 2011 12:59:12 -0400
Subject: [PATCH] #227677: drupal_write_record can't update a column to NULL.

---
 includes/common.inc |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index 075a840..633f159 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -3507,6 +3507,18 @@ function drupal_write_record($table, &$object, $update = array()) {
         $values[] = serialize($object->$field);
       }
     }
+    else if ((drupal_property_exists($object, $field)) && (is_null($object->$field))) {
+      // Handle special case: setting a field to NULL.
+      $fields[] = $field;
+      if (empty($info['serialize'])) {
+        $placeholders[] = '%s';
+        $values[] = 'NULL';
+      }
+      else {
+        $placeholders[] = db_type_placeholder($info['type']);
+        $values[] = serialize($object->$field);
+      }
+    }
   }
 
   // Build the SQL.
@@ -3778,6 +3790,29 @@ function drupal_flush_all_caches() {
 }
 
 /**
+ * Determine if a property exists on a given class. This function is used in
+ * place of property_exists() as it was only introduced in PHP 5.1.
+ *
+ * @param $class
+ *   The class to check to see if a given property exists.
+ * @param $property
+ *   The property to check to see if it exists on the given class.
+ *
+ * @return
+ *   TRUE if the property exists, or FALSE otherwise.
+ */
+function drupal_property_exists($class, $property) {
+  if (is_object($class)) {
+    $vars = get_object_vars($class);
+  }
+  else {
+    $vars = get_class_vars($class);
+  }
+
+  return array_key_exists($property, $vars);
+}
+
+/**
  * Helper function to change query-strings on css/js files.
  *
  * Changes the character added to all css/js files as dummy query-string,
-- 
1.7.4.1

