Hi,

I like the Computed Field module... it is definitely powerful, however I have a problem (which is mentioned in the documentation) but I am hoping there is a simple solution.

I have a Computed Field - Total Amount which appears on Node X, which is actually the sum of all the amounts that have been entered against Node Y. I want to be able to use the Total Amount field in Views and tables so I have checked the store in database box.

My problem is that when I add additional amounts into Node Y, and then go and View the associated Node X record, the Total Amount does not reflect the additional amount.

Of course, if I go and "touch" the Node X record and save it, the Total Amount is now correct.

Is there a solution to allow the Total Amount field to truly reflect the Total Amount of all Node Y records?

thanks in advance..

Comments

fmesco’s picture

Not sure if this will work in your scenario or not, but I had a similar problem and I was able to use the Rules module to trigger a "Save Node" action which recalculated my computed fields. I think the trick for you would be to do the Save Node action on a group of nodes rather than just one... just an idea.

netentropy’s picture

i wonder if there is function that can be added to this module that would help recompute

ozecho’s picture

Love the module... but I have the same need...

It would be great to be able to call a function from a NodeY_insert to update the Node X computed fields (based on the original example above)....

stif’s picture

Hi folks,

I am interested in this functionality as well.

In the meantime i helped myself with the Rules Modul - which exectutes the following Custom PHP Code i found somewhere here (need to activate PHP Filter in Drupal Modules):

// This query is used to load all the nids of a specific content type into an array. 
$query = 'SELECT nid FROM content_type_tipps';

// Create an array to store the query info
$tippsnidarray = array();

//exectue the query using db abstraction stuff
$tippsnidarray = db_query($query);

// For each nid this will load and then save the node
while ($nodeID = db_fetch_array($tippsnidarray)){

//This time limit is important, if it is removed the query will exceed the 30sec limit.
set_time_limit(5);

$PNID = node_load($nodeID["nid"]);
node_save($PNID);

}
// Just so you know its done!
//print 'done';
vacilando’s picture

The fact that Computed Field does not recalculate values forces me to use the otherwise duplicate module Dynamic Field (see http://drupal.org/node/372314).

Hoping to use just one module in the future (Computed Field) after this is fixed. Subscribing.

Moonshine’s picture

Category: bug » support

"values" in db stored computed fields are computed during node save and are not re-computed continually on the fly (which would also involve saving them back to the node every time). Also CCK caches node object values for faster performance.

However, remember you do have "display" code to work with which is always run before the display output of that that value. If you get created with your display code a lot can be done. Just be careful if your code involves expensive operations like node_load.

vacilando’s picture

Since the values supplied to Computed Field may keep changing, I believe it is reasonable to expect from Computed Field to allow re-computing the resulting value - whether it happens with each node load or whether it is cached and how (this could be definable). Computing the final value just once at the time of simply is not enough in many use cases.

The question of the computation impact on performance is valid but it is a separate - it is on the admins to decide what's acceptable for their particular cases. The Computed Field should not be doing the decision for them.

Moonshine’s picture

@vacilando:

I'm not sure I'm following you completely. If values that a computed field is using are changing then they are external to the node it's on. If they are values from a secondary node(s) then a hook_nodeapi call to node_save the related computed nodes would trigger re-caclulation and retain the benefits of core cck node caching and Views use (if db saved). Or if someone truly wanted the computed field to re-calculate every time it's displayed they could just move the bulk of the "computation" code to be "display" code. By having the two sets of code (computation & display) and optional DB saving, computed field isn't really making any "decisions" for the developer outside of it being part of the cck field framework. If I'm missing something, perhaps an example of a specific use case would help, as currently I feel like the bases are pretty well covered.

benallfree’s picture

@Moonshine

Great summary. I hope I can provide a use case that illustrates a need for node_save(). I'd be thankful if you could supply some (pseudo) code for node_save() to address this use case.

I have two content types, Project and Job. Jobs are related to Projects through a node reference. I'd like to be able to display aggregate information about the Jobs belonging to a Project from both a View and from a single node page. As I understand it now, the only way to display a dynamic value in both a view and a single node page is to have it in a custom field. If there is another solution, perhaps that is my answer although the programmer in me really wants this to be a property that follows Project wherever it may go.

Just for clarity, I've added the non-Drupal/non-CCK raw SQL query that I would normally write in these situations. A few notes:

1. I've included an average to force us to do a full recalc from node_save().
2. A few of the calculated project fields are dependent on other calculated project fields.

Project fields:
Title
Description
Total Budget
Job Count - (SELECT count(id) FROM jobs WHERE project_id = projects.id)
Cost Per Job - (SELECT avg(cost) FROM jobs WHERE project_id = projects.id)
Completed Cost - (SELECT sum(id) FROM jobs WHERE project_id = projects.id and status = 'Completed')
WIP Cost (sum of incomplete job costs) - (SELECT sum(id) FROM jobs WHERE project_id = projects.id and status <> 'Completed')
Budget Used (Completed + WIP)
Budget Remaining (Total - Used)

Job
Project ID (noderef)
Title
Description
Cost
Status (Complete/Incomplete)

benallfree’s picture

Quick update to my own question posted to Moonshine :)

I think we can do this using the Rules module with PHP Execution enabled in the core.

I was able to set up a rule to trigger on save/update of the Job content type and dump the node object's contents.

With a little sleuthing, I'm sure I can figure out how to look up the node reference to the Project and trigger the update.

If you look at regenerate.zip in http://drupal.org/node/195013, I think you'll find some sample code snippets that show how to trigger the Project object to recalculate.

jfine’s picture

Or if someone truly wanted the computed field to re-calculate every time it's displayed they could just move the bulk of the "computation" code to be "display" code.

Unfortunately that doesn't work for me because computed field doesn't seem to be able to access the node object in the display code.

roball’s picture

Title: CCK Computed Field doesnt re-compute... » How to re-compute a Computed Field on node view
Version: 6.x-1.0-beta3 » 6.x-1.x-dev

The current 6.x-1.x-dev should make $node directly available to the Display format code. MacaroniDuck, did you try it?

roball’s picture

Title: How to re-compute a Computed Field on node view » How to re-compute a Computed Field at displaying it
feorean’s picture

Thanks a lot vacilando!

This module (Dynamic Field) is very useful. (Better than Computed Field!)

roball’s picture

@feorean: You are here in the issue queue of Computed Field (not Dynamic Field)! And this thread was trying to explain that there is nothing that Dynamic Field does what Computed Field doesn't.

mmjvb’s picture

Issue summary: View changes
Status: Active » Closed (outdated)