How To Insert and Update Only Specific Fields Of Your Entity In Drupal 7

Time from time you need to update single or several specific fields of your entity in Drupal. It can be node, comment, taxonomy or some other custom entity. So how do we usually do this? Let's see:



$node = node_load(1); // 1 is node id we want to load
 
// Change field value
$node->field_product_description[$node->language][0]['value'] = 'This is a very nice and useful product. Buy it now! We want your money!';
 
// Save updated node
node_save($node);

This approach is totally ok, but hey, your poor Drupal did a massive job to update just one field: it loaded the whole node into memory and then, saved every field (even which wasn't updated) one by one into database. If your nodes are pretty big, it can produce a lot of unnecessary queries.

Here's how to update entity field in a more "diet" way by using Drupal 7 Field API, this is a general code template:

// Get the numeric id of your field by passing field name
$info = field_info_field('field_name_goes_here');
$fields = array($info['id']);
 
// Execute the storage function
field_sql_storage_field_storage_write('entity_type', $entity, 'update', $fields);
 
// Clear field cache
cache_clear_all("field:$entity->type:$entity->id", 'cache_field');

And here's how to use it on practice:

$node = node_load(1); // 1 is node id we want to load
 
// Change field value
$node->field_product_description[$node->language][0]['value'] = 'This is a very nice and useful product. Buy it now! We want your money!';
 
// Get the numeric id of your field by passing field name
$info = field_info_field('field_product_description');
$fields = array($info['id']);
 
// Execute the storage function
field_sql_storage_field_storage_write('node', $node, 'update', $fields);
 
// Clear field cache
cache_clear_all("field:node:$node->nid", 'cache_field');

So now, we updated only one field of the node without updating the whole node. Please note, that you can pass as many fields as you like via $fields array, so you can update more than one entity field at once.

That's all for now, thanks for reading this. Share with others!

A VERY BIG NOTE: Using this technique you can't rely on drupal hooks system. No hooks are being invoked, updated nodes won't be queued for search indexing if you won't do this manually. Be awared.

Comments

Submitted by Yuriy Babenko on Mon, 2012-10-22 21:42

Nice tip, thanks!

Submitted by Netdream on Tue, 2012-10-23 13:09

Good tip, but be careful! I suspect that no hooks are executed in this way...

Submitted by Dalin on Wed, 2012-10-24 20:59

This article needs a BIG caveat that this is pretty much on-par with saving values directly to the DB. You'll manually need to clear caches, queue the node for search indexing, and anything else that is normally accomplished by the hook system.

Submitted by Tim on Fri, 2012-10-26 13:59

Makes sense. Added a note about this.

Submitted by Juan on Sun, 2013-04-14 12:21

Awesome!!! just what i was looking for, will this work if i use this instead
function hook_field_storage_write ?????

Submitted by berimbolo on Mon, 2014-02-10 07:48

I've been searching for ages for this!!!
Thank you so much for this post!

Submitted by Johan Haest on Thu, 2014-09-11 00:37

The cache stuff saved my day!

Add new comment

You are here