Drupal 7 tip : Update and insert only specific fields of your entity

Posted by: 
Dominique De Cooman

If you want to save only a specific field on your entity instead of saving the whole entity you can. In a submodule of the fields module you ll find a module called field_sql_storage module. In that module all functions available for the sql storage backend can be used. One of them is field_sql_storage_field_storage_write, which you give your entity, type, operator (update, insert) and which fields to save.

<?php
//Get the id of your field
$name 'name_of_your_field';
$info field_info_field($field_name);
$fields = array(info['id']); 

//Execute the storage function
field_sql_storage_field_storage_write('model'$entity'update'$fields);
?>

This can be a huge gain in performance since you only save the specific fields needed instead of saving the entire entity with all its fields.

Comments

Drupal 7 tip : Update and insert only specific fields of your entity

I think these corrections are needed:

<?php
//Get the id of your field
$field_name = 'name_of_your_field';
$info = field_info_field($field_name);
$fields = array($info['id']);

//Execute the storage function
field_sql_storage_field_storage_write('model', $entity, 'update', $fields);
?>

Drupal 7 tip : Update and insert only specific fields of your entity

It's also worth noting that 'model' should be the $entity_type.

Add new comment