When using node_save, how sure can one be that it did what it was supposed to do?

At the moment after any call to node_save I have been checking that the required property did succesfully update. For example:

$node->field_some_field['und'][0] = array('tid' => $some_taxonomy);

if($node->field_some_field['und'][0]['tid'] == $some_taxonomy) {
    return TRUE;
} else {
    return FALSE;

Is this necessary? Or is there a better way to perform the check?

Edit: As an example, say I want to update a node's taxonomy programatically - it would be useful to know if it worked or not. I think it's just bad in general for things to fail silently.

  • 2
    +1 to Jimajammas answers, but I think you could provide some more context to this. Why would it be necessary to do the check? I've never seen even a remotely similar requirement on any of my sites.
    – Letharion
    Commented Aug 9, 2012 at 6:52

1 Answer 1


I think you have to believe that node_save() is doing its job. But, if you don't, node_save($node) is declared as sending $node by reference, so, if anything changes $node during the node_presave() hook, it will be reflected in $node after the fact. That notwithstanding, if you wanted to be perfectly sure, you could do something like:

node_load(NULL, NULL, TRUE);

if ($node->.....

which would clear any node caches in your page and then reload the node from the database before your if check.


From your example, the failure logic imho should be in your generation of the taxonomy value, not in worrying about if node_save() has or hasn't done its job. For example:

   // assuming we have $node populated


   if ($some_taxonomy !== FALSE) { 
     $node->field_some_field['und'][0] = array('tid' => $some_taxonomy);
   } else {

where your function looks something like this:

   function programmatically_created_tid($args) {

     // do whatever logic is necessary to generate the tid

     if ($success) { // success 
       return $tid;
     } else {
       return FALSE;

would alert the user to the problem you are actually concerned about.

  • Would this not only work if node_save() is creating the object, not updating it? If node_save() has failed to update a pre-existing node, it will still load. Is there not a timestamp ("last_updated" or something) in the node object? If so, wouldn't it be possible to check this timestamp before and after node_save() and see if it has changed? Also, see the answer to this question - you don't need to clear the whole node cache to reload one node from the database. Commented Aug 9, 2012 at 9:21
  • Like @letharion, I've never seen a real world case where node_save() hasn't done its job. But to your first question, this should work for both new and updated nodes as the load may work but if there is a problem, the fields won't be populated correctly in either case. However, if there is such a case, I don't know why you would rely upon $node->changed (the timestamp) to be your validity test instead of checking the value of the field you are most concerned about. As for entity_load_unchanged(), sure enough, that does look like a nifty way of doing things without clearing the page's cache.
    – Jimajamma
    Commented Aug 9, 2012 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.