Download & Extend

file_field_update() triggers a full entity load durung entity_save()

Project:Drupal core
Version:7.x-dev
Component:file.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

file_field_update() (file field's implementation of hook_field_update()) checks if a value has been removed from the previous values, to keep track of file references.

It hardocodes a check on $object->revision. See #644332: Do we need to abstract $object->revision ('new revision') property about that.

More problematic, to compare new values with previous values, it does a full load the current object.

a) I'm not sure the current code is the correct way of doing that - could use the opinion of an entity API guru.

  $load_function = $obj_type . '_load';
  $original = $load_function($id);

(this is the main reason I'm marking this 'critical')

b) entity_save() triggering an entity_load() doesn't sound too great. Reacting on values being removed is a legitimate use case for 'resource reference' field types, I'm wondering if we can make this better.

Comments

#1

subscribe. still getting my head around the fields code, came across this function here #618654: (Fixed, but needs cleanup) File and image fields are treated as temporary files and automatically deleted after six hours.

#2

Priority:critical» normal

Not critical. Please read and understand Priority levels of Issues, thanks.

#3

#733756: image_field_insert() makes no sense and causes a memory leak with devel_generate() was a memory leak on inserts caused by this, although that was fixed by completely removing image_field_insert().

Subscribing to this one.

#4

I think that instead of a full entity load, the code could use field_attach_load() with $options = array('field_id' => $field['id']) to get the current values of the field for the object. This is guaranteed to work whatever the entity type, and would avoid triggering a full entity load() during an entity update().

#5

Status:active» needs review

Quick patch, not tested - I don't know if this part is tested in the current test suite, for that matter.

AttachmentSizeStatusTest resultOperations
file_field_load_on_update.patch1.06 KBIdleFAILED: [[SimpleTest]]: [MySQL] 18,669 pass(es), 7 fail(s), and 0 exception(es).View details

#6

Status:needs review» needs work

The last submitted patch, file_field_load_on_update.patch, failed testing.

#7

Status:needs work» needs review

Oops, f_a_load() works by altering the entity by ref.

AttachmentSizeStatusTest resultOperations
file_field_load_on_update-644338-7.patch1.08 KBIdlePASSED: [[SimpleTest]]: [MySQL] 18,671 pass(es).View details

#8

That works for me, do we want to defer the $revision stuff to #644332: Do we need to abstract $object->revision ('new revision') property?

#9

Title:fragile code in file_field_update()» file_field_update() triggers a full entity load durung entity_save()

Yes, that other issue has more discussions on that topic.

#10

Status:needs review» reviewed & tested by the community

RTBC, then.

#11

Status:reviewed & tested by the community» fixed

Committed to CVS HEAD. Thanks.

#12

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.