Hi all,
I have a very strange case in new developpement of an site web. I had made a block (with a custom module) and I make a node_load. Here all is ok, the node is load, the block is display. But if I change nid of node (by exemple set null) and do nothing else (no node_save, no function call, NOTHING with this object), I have some notice of different module (like 3 or 4 modules). I know it's just notice, but I would understand why the simply fact of change variables wich is never uses, trigger some error..... Exemple :

   $tmp = node_load(arg(1));
$tmp->nid=null;

=> error (lot of "Trying to get property of non-object in..." in diffrent name function)

   $tmp = node_load(arg(1));
$node=$tmp;
$node->nid=null;

=> error (lot of "Trying to get property of non-object in..." in diffrent name function)

    $tmp = node_load(arg(1));
    $node = new stdClass();
    //Copy via une boucle sinon on a des erreurs.
    foreach ($tmp as $key => $value) {
      if ($key != 'nid') $node->{$key} = $value;
    }

=> all is ok wtf o_O

Comments

nevets’s picture

Your code makes some assumptions which can lead to PHP errors, I would write

   $tmp = node_load(arg(1));
   $tmp->nid=null;

as

if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
  if ( !empty($node) ) {
     // We really have a node, do something with it
     // for example
     $node->nid = null;
   }
}
musa.thomas’s picture

No the node exist I'm sure. I put a exemple that you can reproduce on every install :
Create a node type like 'test'.
Create an module with an extra_field for example :

function my_module_field_extra_fields() {
  $extra ['node'] ['test'] ['display'] = array (
    
    'test_simply' => array (
      'label' => t('Test'), 
      'description' => t('Testt'), 
      'weight' => 0 
    ));
  return $extra;

}

then a node_view for example :

/**
 * Implementation of hook node view
 *
 * @param $node object         
 * @param $view_mode string         
 * @param $langcode string         
 */
function my_module_node_view($node, $view_mode, $langcode) {
  
  if ($node->type == 'test' && $view_mode == 'full') {
    $node->content ['test'] = array (
          '#markup' => 'toto'
        );
      
}
}

Ok here all is ok we got an extra field wich display toto. Ok now let's try some basic php, always in extra field :

function my_module_node_view($node, $view_mode, $langcode) {
  
  if ($node->type == 'test' && $view_mode == 'full') {
    $node->content ['test'] = array (
          '#markup' => 'toto'
        );
//Copy node
$new_node=$node;
$new_node->nid=null;
      //Do nothing with $new_node but trigger error
}
}

=> Wtf : Fatal error!

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'nid' cannot be null: INSERT INTO {history} (uid, nid, timestamp)

Realy strange behaviours .... and same strange if you do (like i said at top) the same in a block with a node_load (no fatal error but lot of notice)