How to to write Computed_field code? I'm testing the computed_field module on a taxonomy field. I want to get the number of nodes that have a term reference to the current term:

$tid = 1;
$entity_field[0]['value'] =  db_query("
SELECT count(*)
FROM node n INNER JOIN taxonomy_index t on n.nid = t.nid
WHERE n.type = 'discussion'
and n.status = 1
and t.tid=$tid")-&gt;fetchField();


How can I get the tid of the term? Is there a better way of writing the query?
A query like that should do the trick, but if you want something more elegant, I recommend EntityFieldQuery.