Posted by Emerya.thomas on November 14, 2012 at 5:21pm
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 :
<?php
$tmp = node_load(arg(1));
$tmp->nid=null;
?><?php
$tmp = node_load(arg(1));
$node=$tmp;
$node->nid=null;
?><?php
$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;
}
?>
Comments
Your code makes some
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;
}
}
No the node exist I'm sure. I
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 :
<?php
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 :
<?php
/**
* 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 :
<?php
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)