Issue #970338 by jbrown, joachim: Fixed drupal_write_record() cannot save an empty (full default) row.

8.0.x
catch 2011-12-05 21:40:54 +09:00
parent c73e4a3b4f
commit 51dd3e20d3
2 changed files with 14 additions and 5 deletions

View File

@ -6901,6 +6901,7 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
$object = (object) $record;
$fields = array();
$default_fields = array();
// Go through the schema to determine fields to write.
foreach ($schema['fields'] as $field => $info) {
@ -6923,6 +6924,7 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
if (!property_exists($object, $field)) {
// Skip fields that are not provided, default values are already known
// by the database.
$default_fields[] = $field;
continue;
}
@ -6953,10 +6955,6 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
}
}
if (empty($fields)) {
return;
}
// Build the SQL.
if (empty($primary_keys)) {
// We are doing an insert.
@ -6973,10 +6971,16 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
unset($fields[$serial]);
}
}
$query = db_insert($table, $options)->fields($fields);
// Create an INSERT query. useDefaults() is necessary for the SQL to be
// valid when $fields is empty.
$query = db_insert($table, $options)
->fields($fields)
->useDefaults($default_fields);
$return = SAVED_NEW;
}
else {
// Create an UPDATE query.
$query = db_update($table)->fields($fields);
foreach ($primary_keys as $key) {
$query->condition($key, $object->$key);

View File

@ -1962,6 +1962,11 @@ class DrupalDataApiTest extends DrupalWebTestCase {
* Test the drupal_write_record() API function.
*/
function testDrupalWriteRecord() {
// Insert a record with no columns populated.
$record = array();
$insert_result = drupal_write_record('test', $record);
$this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when an empty record is inserted with drupal_write_record().'));
// Insert a record - no columns allow NULL values.
$person = new stdClass();
$person->name = 'John';