Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

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

Dominique De Cooman
Tuesday, June 21, 2011 - 12:09
fields [1]
performance [2]
entities [3]
d7tip [4]

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.


Source URL: https://dominiquedecooman.com/blog/drupal-7-tip-update-and-insert-only-specific-fields-your-entity

Links
[1] https://dominiquedecooman.com/blog-topics/fields-0
[2] https://dominiquedecooman.com/blog-topics/performance
[3] https://dominiquedecooman.com/blog-topics/entities
[4] https://dominiquedecooman.com/blog-topics/d7tip