force node to reflect change made by db query?

hoppurr - April 5, 2009 - 20:46

simple program to update a field in a cck content type

if (condition) {
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", $text,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
} else {
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
}

the program works and the update shows when the views module reads the field to display a node list, but when i click on the link to the node, the change is NOT shown to the field even though the field is set to be visible in the node.. how can i fix this?
the whole program is as follows
if i go into the cck admin, and save the field, i can get it to reflect the update, but that has to be done every time i run the program
<?php

/*******************************************************************************************************************************************
*                                                  DRUPAL HOOKS (CALLED BY DRUPAL'S CORE)                                                 *
*******************************************************************************************************************************************/
/**
  * Implementation of hook_perm()
  */
function on_special_perm() {
  return array('view on_special_info');
}
/**
  * Gmplementation of hook_block()
  */
function on_special_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
    case 'list':
$blocks[0]['info'] = t('on_special_info');
    return $blocks;
case 'view':
  switch($delta) {
case 0:
if (user_access('view on_special_info')) {
$reader=reader();
$series_grab=$reader[0];
$series_title=ereg_replace("_"," ",ucfirst($series_grab));
$block['subject'] = "";
$block['content'] = special();
break;
        }
         }
return $block;
}
}

/*****************************************************************************************************************************************
*                                                         END DRUPAL HOOKS                                                              *
*****************************************************************************************************************************************/
function special() {
$text="<font color=red>On Sale</font>";
$node=node_info();
if (isset($_POST['yesno'])) {
db_query("UPDATE {content_type_stock_item} SET field_special_value  = '%s' WHERE nid = %d", $_POST['yesno'],$node->nid);
}
if ($_POST['price'] != "" and $_POST['yesno'] == "yes") {
db_query("UPDATE {content_type_stock_item} SET field_sale_price_value  = '%s' WHERE nid = %d", $_POST['price'],$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", $text,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
} else if ($_POST['yesno']=="yes") {
$error="Please Set a Price<br>";
db_query("UPDATE {content_type_stock_item} SET field_sale_price_value  = '%s' WHERE nid = %d", $_POST['price'],$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_special_value  = '%s' WHERE nid = %d", "no",$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
} else if ($_POST['yesno']=="no") {
db_query("UPDATE {content_type_stock_item} SET field_sale_price_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
}
    $special = db_fetch_array(db_query('SELECT field_special_value FROM {content_type_stock_item} WHERE nid = %d', $node->nid));
    $checked = $special['field_special_value'];
    $price_get = db_fetch_array(db_query('SELECT field_sale_price_value FROM {content_type_stock_item} WHERE nid = %d', $node->nid));
    $price = $price_get['field_sale_price_value'];
if ($checked == "yes") {
$yescheck="CHECKED";
$nocheck="";
} else if ($checked == "no") {
$yescheck="";
$nocheck="CHECKED";
} else if ($checked == NULL) {
db_query("UPDATE {content_type_stock_item} SET field_special_value  = '%s' WHERE nid = %d", "no",$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
$yescheck="";
$nocheck="CHECKED";
}
$blocka=$error."On Special?<form name=\"special\" method=\"POST\" action=".$PHP_SELF.">
<input type=\"radio\" ".$all." name=\"yesno\" value=\"yes\" ".$yescheck.">Yes
<input type=\"radio\" ".$all." name=\"yesno\" value=\"no\"".$nocheck.">No
&nbsp;&nbsp;&nbsp;Price:<input type=\"text\" size=\"10\" name=\"price\" value=".$price.">
<input type=\"submit\" name=\"do\" value=\"Set\" ></input>
        </form>
";
return $blocka;
}

node_load($node->nid,null,TRUE)

grobemo - April 5, 2009 - 20:58

This is just a hunch, but it's probably the internal node cache that's causing you trouble.

After you run the UPDATE queries, reload the node and force Drupal to reset the internal node cache by using:

<?php
  $node
= node_load($node->nid,null,TRUE);
?>

(See the documentation for node_load() for details.)

you mean like this?

hoppurr - April 5, 2009 - 21:21

you mean like this?
still no dice

<?php

/*******************************************************************************************************************************************
*                                                  DRUPAL HOOKS (CALLED BY DRUPAL'S CORE)                                                 *
*******************************************************************************************************************************************/
/**
  * Implementation of hook_perm()
  */
function on_special_perm() {
  return array('view on_special_info');
}
/**
  * Gmplementation of hook_block()
  */
function on_special_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
    case 'list':
$blocks[0]['info'] = t('on_special_info');
    return $blocks;
case 'view':
  switch($delta) {
case 0:
if (user_access('view on_special_info')) {
$reader=reader();
$series_grab=$reader[0];
$series_title=ereg_replace("_"," ",ucfirst($series_grab));
$block['subject'] = "";
$block['content'] = special();
break;
        }
         }
return $block;
}
}

/*****************************************************************************************************************************************
*                                                         END DRUPAL HOOKS                                                              *
*****************************************************************************************************************************************/
function special() {
$text="<font color=red>On Sale</font>";
$node=node_info();
if (isset($_POST['yesno'])) {
db_query("UPDATE {content_type_stock_item} SET field_special_value  = '%s' WHERE nid = %d", $_POST['yesno'],$node->nid);
  $node = node_load($node->nid,null,TRUE);
}
if ($_POST['price'] != "" and $_POST['yesno'] == "yes") {
db_query("UPDATE {content_type_stock_item} SET field_sale_price_value  = '%s' WHERE nid = %d", $_POST['price'],$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", $text,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
  $node = node_load($node->nid,null,TRUE);
} else if ($_POST['yesno']=="yes") {
$error="Please Set a Price<br>";
db_query("UPDATE {content_type_stock_item} SET field_sale_price_value  = '%s' WHERE nid = %d", $_POST['price'],$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_special_value  = '%s' WHERE nid = %d", "no",$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
  $node = node_load($node->nid,null,TRUE);
} else if ($_POST['yesno']=="no") {
db_query("UPDATE {content_type_stock_item} SET field_sale_price_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
  $node = node_load($node->nid,null,TRUE);
}
    $special = db_fetch_array(db_query('SELECT field_special_value FROM {content_type_stock_item} WHERE nid = %d', $node->nid));
    $checked = $special['field_special_value'];
    $price_get = db_fetch_array(db_query('SELECT field_sale_price_value FROM {content_type_stock_item} WHERE nid = %d', $node->nid));
    $price = $price_get['field_sale_price_value'];
if ($checked == "yes") {
$yescheck="CHECKED";
$nocheck="";
} else if ($checked == "no") {
$yescheck="";
$nocheck="CHECKED";
} else if ($checked == NULL) {
db_query("UPDATE {content_type_stock_item} SET field_special_value  = '%s' WHERE nid = %d", "no",$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_value  = '%s' WHERE nid = %d", NULL,$node->nid);
db_query("UPDATE {content_type_stock_item} SET field_on_sale_format  = %d WHERE nid = %d", 3,$node->nid);
  $node = node_load($node->nid,null,TRUE);
$yescheck="";
$nocheck="CHECKED";
}
$blocka=$error."On Special?<form name=\"special\" method=\"POST\" action=".$PHP_SELF.">
<input type=\"radio\" ".$all." name=\"yesno\" value=\"yes\" ".$yescheck.">Yes
<input type=\"radio\" ".$all." name=\"yesno\" value=\"no\"".$nocheck.">No
&nbsp;&nbsp;&nbsp;Price:<input type=\"text\" size=\"10\" name=\"price\" value=".$price.">
<input type=\"submit\" name=\"do\" value=\"Set\" ></input>
        </form>
";

return $blocka;
}

Yes, that's what I meant, but

grobemo - April 5, 2009 - 21:30

Yes, that's what I meant, but I guess it's not the right solution.

What's node_info()?

oh that was a call to another

hoppurr - April 5, 2009 - 22:06

oh that was a call to another function in another module

function node_info() {
if ( arg(0) == 'node' or arg(0) == 'print' && is_numeric(arg(1)) && ! arg(2) ) {
  $node = node_load(arg(1));
}

if only i knew what function was called by cck when saving field settings, and what arguments to pass to it that might work.

.

grobemo - April 5, 2009 - 22:14

If it's really a CCK issue, it may well be beyond me. But while we're trying to figure it out, two questions:

1. When and where, exactly, does this block get displayed for which you're calling these functions?

2. Have you considered using node_save() instead of editing the database directly? It might be that CCK or some other module is using that to overwrite your changes.

Indeed using node_save($node)

danielb - April 7, 2009 - 11:33

Indeed using node_save($node) would avoid all these problems.
If you want to get specific about it, it might be enough to call these two functions:

content_presave($node);
content_update($node);

no idea if that will work though.

 
 

Drupal is a registered trademark of Dries Buytaert.