0
votes

i have a module and i am using node_load(array('nid' => arg(1))); now the problem is that this function keep getting its data for node_load from DB cache.
how can i force this function to not use DB cache?
Example
my link is http://mydomain.com/node/344983
now:
$node=node_load(array('nid'=>arg(1)),null,true);
echo $node->nid . " -- " arg(1);
output
435632 -- 435632
which is a randomly node id (available on the system)
and everytime i ctrl+F5 my browser i get new nid!!

Thanks for your help

4
if this is in a module, please post your _menu hooksAndrew Sledge
function custom_node_menu(){ $items = array(); $items['node'] = array ( 'title' => 'node', 'page callback' => 'drupal_not_found', 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); return $items; }Alaa
Does node_load(arg(1)) show similar issues? What does print arg(1) output? Is it possible you've got caching on for anonymous users and you're just seeing an earlier output from the page?ceejayoz
it is written above (435632),,, the problem is that arg(1) getting incorrect value... node_load doesn't have any problem when you use $reset=true but the problem is with arg api itself!!! i dont know why it gets incorrect values!!Alaa
I very much doubt the issue is with arg() itself, or Drupal would cease to function entirely. See my edit regarding caching - try clearing the cache with devel.module.ceejayoz

4 Answers

3
votes

Where are you calling this? For example, are you using it as part of your template.php file, as part of a page, or as an external module?

Unless you have this wrapped in a function with its own namespace, try naming the variable differently than $node -- for example, name it $my_node. Depending on the context, the 'node' name is very likely to be accessed and modified by Drupal core and other modules.

If this is happening inside of a function, try the following and let me know what the output is:

$test_node_1 = node_load(344983); // Any hard-coded $nid that actually exists
echo $test_node_1->nid;

$test_node_2 = node_load(arg(1)); // Consider using hook_menu loaders instead of arg() in the future, but that's another discussion
echo $test_node_2->nid;

$test_node_3 = menu_get_object(); // Another method that is better than arg()
echo $test_node_3->nid;

Edit:

Since you're using hook_block, I think I see your problem -- the block itself is being cached, not the node.

Try setting BLOCK_NO_CACHE or BLOCK_CACHE_PER_PAGE in hook_block, per the documentation at http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_block/6

You should also try to avoid arg() whenever possible -- it's a little bit of a security risk, and there are better ways to accomplish just about anything arg() would do in a module environment.

Edit:*

Some sample code that shows what I'm referring to:

function foo_block ($op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
      case 'list':
        $blocks[0] = array(
          'info' => 'I am a block!',
          'status' => 1,
          'cache' => BLOCK_NO_CACHE // Add this line
        );
        return $block;
      case 'view':
       .....
    }
}
0
votes

node_load uses db_query, which uses mysql_query -- so there's no way to easily change the database's cache through that function.

But, node_load does use Drupal's static $nodes cache -- It's possible that this is your problem instead of the database's cache. You can have node_load clear that cache by calling node_load with $reset = TRUE (node_load($nid, NULL, TRUE).

Full documentation is on the node_load manual page at http://api.drupal.org/api/drupal/modules--node--node.module/function/node_load/6

0
votes

I have had luck passing in the node id to node_load not in an array.

node_load(1);

According to Druapl's api this is acceptable and it looks like if you pass in an array as the first variable it's loaded as an array of conditions to match against in the database query.

0
votes

The issue is not with arg(), your issue is that you have caching enabled for anonymous users.

You can switch off caching, or you can exclude your module's menu items from the cache with the cache exclude module.

edit: As you've now explained that this is a block, you can use BLOCK_NO_CACHE in hook_block to exclude your block from the block cache.