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
Comment | File | Size | Author |
---|---|---|---|
#7 | 1833492-cck-content_module_update_cck_to_null_v3.patch | 1.46 KB | Anonymous (not verified) |
#6 | 1833492-cck-content_module_update_cck_to_null_v2.patch | 1.5 KB | Anonymous (not verified) |
#2 | 1833492-cck-content_module_update_cck_to_null.patch | 715 bytes | Anonymous (not verified) |
#2 | 1833492-content_module_update_cck_to_null.patch | 968 bytes | Anonymous (not verified) |
#1 | 1833492-cck-content_module_update_cck_to_null.patch | 0 bytes | Anonymous (not verified) |
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #3
markdorisonComment #4
apotek CreditAttribution: apotek commented@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.
Comment #4.0
Anonymous (not verified) CreditAttribution: Anonymous commentedAdd details to description
Comment #5
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #5.0
Anonymous (not verified) CreditAttribution: Anonymous commentedadd similar / related issues
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous commentedUpdated 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.
Comment #6.0
Anonymous (not verified) CreditAttribution: Anonymous commentedadd description
Comment #7
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #7.0
Anonymous (not verified) CreditAttribution: Anonymous commentedAdd more detailed example
Comment #7.1
Anonymous (not verified) CreditAttribution: Anonymous commentedAdding another example