Drupal how to load content fields using cck content api avoiding node load
How to load only the content fields of a node to avoid a full node load?
Sometimes in scripts you need information stored in cck fields but you dont want to do full node loads neither do you want to query the table directly because setting a field from single to multiple value would make the table location of that field change.
For example a field in the content_type_team named field_team_name_value its table would change to content_field_team_name
<?php
//So this is wrong
$content = db_fecth_object(db_query("SELECT * FROM content_type_team" WHERE nid = %d, 1));
$team_name = $content->field_team_name;
//wont output nothing when field was set to multiple
?>
The content (ckk) api has a function to get all fields in just one call: content_storage($op, $node)
<?php
//This is correct
$node = stdClass();
$node->nid = 1;
$node->vid = db_result(db_query("SELECT vid FROM {node} WHERE nid = %d",1));
$node->type = 'team';
$content = content_storage('load', $node);
$team_name = $content['field_team_name'][0]['value'];
?>
Comments
Drupal how to load content fields using cck content api avoiding node load
Good tip.
In case you only want to retrieve one field, you could still go with your first option. You can get the table and field name dynamically as follows:
$field = content_database_info(content_fields('field_team_name'));
table is in $field['table']
optionally column in $field['columns']
Add new comment