From 210bc9726ffd8b0019ccad31ee0939a0d3488c01 Mon Sep 17 00:00:00 2001
From: Dave Hall <dave.hall@skwashd.com>
Date: Fri, 8 Apr 2011 20:56:22 +1000
Subject: [PATCH] Issue #1120412: comments support additional columns

---
 modules/comment/comment.module |   51 +++++++++++++++-------------------------
 1 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 9ab70c5..89d1f7e 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -1467,21 +1467,14 @@ function comment_save($comment) {
     module_invoke_all('entity_presave', $comment, 'comment');
 
     if ($comment->cid) {
-      // Update the comment in the database.
-      db_update('comment')
-        ->fields(array(
-          'status' => $comment->status,
-          'created' => $comment->created,
-          'changed' => $comment->changed,
-          'subject' => $comment->subject,
-          'uid' => $comment->uid,
-          'name' => $comment->name,
-          'mail' => $comment->mail,
-          'homepage' => $comment->homepage,
-          'language' => $comment->language,
-        ))
-        ->condition('cid', $comment->cid)
-        ->execute();
+      // We ignore some fields on update
+      $cloned_comment = clone($comment);
+      $ignored = array('nid', 'pid', 'thread', 'hostname');
+      foreach ($ignored as $ignore) {
+        unset($cloned_comment->{$ignore});
+      }
+
+      drupal_write_record('comment', $cloned_comment, 'cid');
 
       // Ignore slave server temporarily to give time for the
       // saved comment to be propagated to the slave.
@@ -1553,23 +1546,17 @@ function comment_save($comment) {
         $comment->name = $user->name;
       }
 
-      $comment->cid = db_insert('comment')
-        ->fields(array(
-          'nid' => $comment->nid,
-          'pid' => empty($comment->pid) ? 0 : $comment->pid,
-          'uid' => $comment->uid,
-          'subject' => $comment->subject,
-          'hostname' => ip_address(),
-          'created' => $comment->created,
-          'changed' => $comment->changed,
-          'status' => $comment->status,
-          'thread' => $thread,
-          'name' => $comment->name,
-          'mail' => $comment->mail,
-          'homepage' => $comment->homepage,
-          'language' => $comment->language,
-        ))
-        ->execute();
+      // Set the pid value correctly
+      if (empty($comment->pid)) {
+        $comment->pid = 0;
+      }
+
+      // Add missing values to object
+      $comment->thread = $thread;
+      $comment->hostname = ip_address();
+
+      drupal_write_record('comment', $comment);
+      $comment->cid = $comment->cid;
 
       // Ignore slave server temporarily to give time for the
       // created comment to be propagated to the slave.
-- 
1.7.0.4

