If I perform the computed code of:

$node_field[0]['value'] = $node->title;

I see the results in the database. The following code does the same thing:

$output = db_result(db_query("SELECT title FROM {node} WHERE node.nid = %d", $node->nid));
$node_field[0]['value'] = $output;

However, if I try to query other tables in the database for information nothing gets stored in the database. For example, the FeedAPI module has a table {feedapi_node_item_feed} that relates the feed item NID to the feed NID. So, I wrote a code based off the above to put the feed NID into the computed field:

$output = db_result(db_query("SELECT feed_nid FROM {feedapi_node_item_feed} WHERE feedapi_node_item_feed.feed_item_nid = %d", $node->nid));
$node_field[0]['value'] = $output;

However, no information gets stored in the database. I guess I don't understand why one works and the other doesn't. Can anyone give me some advice? What am I missing?

Comments

mkinnan’s picture

Is it plausible that the computed code is running before other modules hook into the node to fill out their tables with the NID? This would explain why I am getting empty results.

mkinnan’s picture

After lots of thinking on how everything works, the computed field can't do the above code because the node is not saved first. Once the node is saved, then the computed field isn't going to run the code. This makes sense to me now.

If anyone else is trying to do something similar, the solution to my problem was to use the Rules module (http://drupal.org/project/rules). All I needed to do was create a rule that ran some PHP code AFTER the node was saved to the database. Then you can access the new rows in the tables in the database that have just been created.

mmjvb’s picture

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