0

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)?

1

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
}
0

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

1
  • Hi, im looking for programatical way. Becouse views object isnt good for performance in this case.
    – Patrick
    Jul 29 '14 at 10:32
0
$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

2
  • what mean 'f' parameter?
    – Patrick
    Jul 29 '14 at 10:59
  • f is just an alias for the field table. I have just updated the name to better reflect how it will be stored in the database
    – pete80
    Jul 29 '14 at 11:11
0

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

  • first thing you need to update it in all the tables (main and revision tables).
  • Second thing, drupal events will not trigger that is attached with updation.

If you still want to do it manually by db_select() and db_query()

Hope it will help!

2
  • In my case, i need to check all nodes of one type just for one field, so its better for performance when i dont "build" full node, sure for write i will get full node
    – Patrick
    Jul 29 '14 at 14:15
  • Then i don't see any other way than custom queries. But you check the fields easily but during updation you need to take care of all the conditions and pattern in which the data need to be wrtite in the tables. Jul 31 '14 at 5:18
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.