Offering a bounty for somebody how can send me the code to calculate a couple of computed cck fields for drupal 6.
Tell me how much you need for this. No need to test it on your own system. Just send me the code to include and I will test it and if necessary get back to you with questions.

I am using node hierarchy to have parent/child relationship between two different cck node types. One node type is "project" and the other is "bid".
The bid contains a field named "price".

Now the parent, project node should contain the following computed fields:

1) "number_bids": counts the number of bids (equivalent to counting the number of children)
2) lowest_price: go through all the bids and find the lowest bid which is not equal to zero
3) lowest_bid_link: a link to the lowest bid node
4) price_difference: the price difference between the highest bid and the lowest bid (lowest bid not equal to zero)

the field "price" is of type "decimal number". Therefore, I expect the computed fields "lowest_price" and "price_difference" also to be of type decimal number. "number_bids" however will be of type integer.

Thanks for your help

Urs

Comments

jastraat’s picture

Since a computed field is only calculated when the node containing it is saved, I think you'll need a computed field in the bid node type as well - to find the corresponding project node and resave it. This will be more complicated if a bid is allowed to change it's parent project. Then - you may need to have a computed field that stores the prior value of the parent project and resaves both the former parent and the new parent nodes (with the updated children counts, etc).

------------------------------
http://fraggles.artsci.wustl.edu (Drupal user documentation and development blog)

Uersu’s picture

Thanks, I was aware of this issue. I first wanted to solve it the easy way and NOT having the fields stored. If they are not stored, then my understanding is that the are recalculated every time that the are viewed. So, when you view the project you see the new computed fields. But I am aware that this will cause the fields not be to available for a view.

So, for now we can ignore this issue.

jastraat’s picture

In case someone is interested - I made a tiny module that recalculates the value of all STORED computed fields every time cron runs (Drupal 6)-

/**
 * Implementation of hook_cron()
 */
function computed_field_cron_cron() {
  $fields = content_fields();
  $types = array();
  foreach($fields as $name => $value){
    $node_type = $value['type_name'];
    if($value['type'] == 'computed' && !in_array($node_type, $types)){
      $types[] = $node_type;
      $result = db_query("SELECT nid FROM {content_type_$node_type}");
      while($n = db_fetch_object($result)){
        $node = node_load($n->nid);
        $node = node_submit($node);
        node_save($node);
      }
    }
  }
  cache_clear_all();
}

------------------------------
http://fraggles.artsci.wustl.edu (Drupal user documentation and development blog)