Is it possible to use a function which uses $node without the expense of node_load
Solutions
You could just write a query yourself if you don't want to load a whole node object.
It should look something like this:
SELECT td.name
FROM {term_data} td
INNER JOIN {term_node} tn ON tn.tid = td.tid
WHERE td.vid = 1
AND tn.nid = 1
Then you can just loop through your results and do whatever you need with them.
I don't have easy access to a Drupal database at the moment, so the query might be slightly out..but it should be enough to get you started :)
In this case, I would just look at the source for taxonomy_node_get_terms_by_vocabulary:
function taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid') {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, $node->vid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
}
return $terms;
}
The only thing needed is $node->vid
. Note that vid
in this particular referes to node revision and not vocabulary ID. If you knew the vid
, then you could just create your own function:
function mytaxonomy_node_get_terms_by_vocabulary($nodevid, $vid, $key = 'tid') {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, nodevid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
}
return $terms;
}
The problem is that in most cases you don't know the $node->vid
without doing a node_load. Just querying using the nid may or may not be right if you use revisions or start using revisions in the futures. The proper way to do it would be to JOIN
the node table into the query so you can use $node->nid
as an argument instead of $node->vid
.
I asked a similar question a couple days ago. Check the answers here and here.