I have x nodes with field named "thefield". My script is checking "thefield" and if he does so the right value is updated.
How can i get from node just one field for check and didnt load full entity (node_load)?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have x nodes with field named "thefield". My script is checking "thefield" and if he does so the right value is updated.
How can i get from node just one field for check and didnt load full entity (node_load)?
I would use a db_query to find all nodes that that field appears on and then update them with a node_load here is the code:
$result = db_query('SELECT f.entity_id FROM {field_data_field_thefield} f');
foreach ($result as $record) {
$node = node_load($record->entity_id);
// update node
}
You can either do it programmatically or using views.
Using Views : Create a view page and under the fields section add the following fields:
Node id and the specific field you would like to update.
Update the filter condition as follows: Node type = 'YOUR NODE TYPE' AND "Your required field" = NOT NULL
The output of this view will be the node ids of all those node which will be having a values assigned to the specific field.
If this didn't helped you, please let me know, will solve it in a different way
Thanks
$nid = 1;
$thefield = db_select('field_data_field_thefield', 'f');
->fields('f',array('entity_id'))
->condition('f.entity_id', $nid, '=');
->execute()
->fetchField();
if($thefield){
Do Something...
}
You could use a db_query instead of db_select to make it even quicker
If you want to update any field in node , then better to use node_load()
function.
Because if you use any custom select or update query then
If you still want to do it manually by db_select()
and db_query()
Hope it will help!
The field API allows you to load/attach just one field in a list of entities with field_attach_load()
. This approach is field storage agnostic, invokes field-level hooks, and reads from the field cache if possible.