Hello,

I develop a Drupal module in order to update a custom CCK field belonging to some nodes; for that I defined a mymodule_cron function in which a SQL query is executed. I have something like:

function mymodule_cron() {
    ...
    db_query("UPDATE node LEFT JOIN... SET ... = ...");
}

To test my function I trigger the cron manually, and I'm sure the function is well called; I'm also sure that the SQL query is correct and that one record is affected by the update: I tested the SQL in myphpAdmin (the CCK field is well modified), and when I trace the result of the db_query in the wathdog I get 1.

The strange thing for me is that, after the execution of the function, I don't see the modification on the CCK field, even if clear all Drupal cache.

I would appreciate any help and idea ;-)

Bertrand.

Comments

ziobudda’s picture

Hi, have you tried with

node_load
change field X in the node loaded
node_save

?

ZioBudda

Freelancer Senior Drupal Developer -- http://www.ziobuddalabs.it

bgoetzmann’s picture

Thank you for your help, my module works now.

This post: "Update CCK Fields In Custom Drupal Nodes" (http://balancedscale.com/blog/201006/update-cck-fields-custom-drupal-nodes) helps me a lot.

Here my cron function:

function mymodule_cron() {
	$sql = "...";	
	$result = db_query($sqlSelect);	
	while ($ns = db_fetch_object($result)) {
		$node = node_load($ns->nid);
		$node->field_starifie[0]['value'] = 'oui'; // field_starifie is my CCK field
		// Prepare node for saving
		$node = node_submit($node);
		// Save Node
		node_save($node);
		// Update content if node exists, otherwise use content_insert()
		content_update($node);

		// Clear CCK data cache so the new value will be loaded for dispaly purposes
		db_query("DELETE FROM {cache_content} WHERE cid = '%s'", 'content:' . $node->nid . ':' . $node->vid);
	}
}

Cheers,

Bertrand
odelia technologies, http://www.odelia-technologies.com

ziobudda’s picture

Great, but (only for a stilistyc code comment) "node_submit" is useless.

From the api for node_submit: "Prepares a node for saving by populating teaser, author, and creation date".
You already have this data.

ZioBudda

Freelancer Senior Drupal Developer -- http://www.ziobuddalabs.it

geordie__’s picture

It sounds strange but this implementation doesn't work if used into a custom action. This action is called into a triggered rule and it seems that node changes are overwritten during the rule execution workflow.