diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-05-20 05:44:03 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-05-20 05:44:03 +0000 |
commit | e5350c1905275af6d5b9df925106cfc7a6f0b12f (patch) | |
tree | dcde2cf5ff4292b36dcc8a7f692f821915f3aa10 /includes/common.inc | |
parent | 9df38a58a4430e544e484e74bba37825914317f0 (diff) | |
download | brdo-e5350c1905275af6d5b9df925106cfc7a6f0b12f.tar.gz brdo-e5350c1905275af6d5b9df925106cfc7a6f0b12f.tar.bz2 |
#445214 by Josh Waihi: Fix drupal_write_record() to correctly deal with NULL serial columns.
Diffstat (limited to 'includes/common.inc')
-rw-r--r-- | includes/common.inc | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/includes/common.inc b/includes/common.inc index 9dd8ecc1d..3b353756c 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -3978,7 +3978,20 @@ function drupal_write_record($table, &$object, $primary_keys = array()) { // Build the SQL. if (empty($primary_keys)) { - $query = db_insert($table)->fields($fields); + $options = array('return' => Database::RETURN_INSERT_ID); + if (isset($serial) && isset($fields[$serial])) { + // If the serial column has been explicitly set with an ID, then we don't + // require the database to return the last insert id. + if ($fields[$serial]) { + $options['return'] = Database::RETURN_AFFECTED; + } + // If a serial column does exist with no value (i.e. 0) then remove it as + // the database will insert the correct value for us. + else { + unset($fields[$serial]); + } + } + $query = db_insert($table, $options)->fields($fields); $return = SAVED_NEW; } else { @@ -3992,8 +4005,14 @@ function drupal_write_record($table, &$object, $primary_keys = array()) { // Execute the SQL. if ($last_insert_id = $query->execute()) { if (isset($serial)) { - // Populate the serial field. - $object->$serial = $last_insert_id; + // If the database was not told to return the last insert id, it will be + // because we already know it. + if (isset($options) && $options['return'] != Database::RETURN_INSERT_ID) { + $object->$serial = $fields[$serial]; + } + else { + $object->$serial = $last_insert_id; + } } } // If we have a single-field primary key but got no insert ID, the |