When updating a cck value to NULL, the existing value in the field is not overwritten.

Example:
When using the migrate module to update an existing node, only fields that have content are migrated. If you are updating a node's field property from a text value "some value" to a NULL value, the text in the destination node is not set to null.

Similar / related issues
http://drupal.org/node/227677 (This issue contains the core patch I am using)
http://drupal.org/node/85929

UPDATE:
An update to the patch is attached. The elseif should be changed to only check for false when drupal_property_exists() is called. Checking for is_set is not necessary because we are checking the $object variable only contains properties that are set in the given node. If a property is being set to NULL, it will return false when drupal_property_exists() is called on it because NULL properties do not exist in $obejct. This code is wrapped in a foreach that loops through each value in $schema['fields']. So, $field will contain every known property on $object, not just the ones that are being set.

Example of an object that is being saved. The object contains 3 fields; x, y & z. z is being set to NULL.

When the object is ready to be save, it only contains values for the fields that have something in them:

object(stdClass)#2725 (22) {
["field_x_value"]=>
string(1) "0"
["field_y_value"]=>
string(1) "0"
["nid"]=>
string(6) "260250"
["vid"]=>
string(6) "260334"
}

When we loop through $schema['fields'] for the given $table, all properties are checked, regardless of their value. If we deliberately put a NULL in any of these fields, it won't be saved using the old code.

vid = 260334
nid = 260250
field_x_value = 0
field_y_value = 0

By adding the else if, the code catches the NULL property and adds it to $values

vid = 260334
nid = 260250
field_x_value = 0
field_y_value = 0
field_z_value =

--------
Another way to reproduce this issue:
- ### is a node id that has a cck field
- "test" is the name of the cck field

$ drush php-eval '
$node = node_load(###);
// Try to write empty space to a cck field.
$node->field_test = array(array("value" => ""));
node_save($node);
echo "\ndone\n";'

Outputs this:
Column 'field_test_value' cannot be null
[warning]
query: INSERT INTO content_field_test (vid, nid,
field_mediarecall_metadata_value) VALUES (###, ###, NULL)
database.mysqli.inc:147

done

CommentFileSizeAuthor
#7 1833492-cck-content_module_update_cck_to_null_v3.patch1.46 KBAnonymous (not verified)
#6 1833492-cck-content_module_update_cck_to_null_v2.patch1.5 KBAnonymous (not verified)
#2 1833492-cck-content_module_update_cck_to_null.patch715 bytesAnonymous (not verified)
#2 1833492-content_module_update_cck_to_null.patch968 bytesAnonymous (not verified)
#1 1833492-cck-content_module_update_cck_to_null.patch0 bytesAnonymous (not verified)
#1 1833492-content_module_update_cck_to_null.patch0 bytesAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Anonymous’s picture

markdorison’s picture

Status: Active » Needs review
apotek’s picture

Issue tags: +NULL cck overwrite

@SpaJenniOs This is an important issue. I'm not sure how to categorize this patch though since it relies on both a core drupal mod as well as a patch to the module. I'm not sure what the correct procedure is in these cases. I'll try to get some input to see how best to handle this.

Anonymous’s picture

Issue summary: View changes

Add details to description

Anonymous’s picture

Anonymous’s picture

Issue summary: View changes

add similar / related issues

Anonymous’s picture

Updated patch for CCK only.

This patch will add an elseif condition to catch were a property exists and is set to NULL. The previous version of this function only checks for the condition where the property is set. If we are trying to update an existing node's property to NULL, isset() returns false and the field is skipped. By checking if the property exists AND the property is set to NULL, we are allowing CCK fields to be updated with NULL values.

Anonymous’s picture

Issue summary: View changes

add description

Anonymous’s picture

Anonymous’s picture

Issue summary: View changes

Add more detailed example

Anonymous’s picture

Issue summary: View changes

Adding another example