0

I am trying to update a node title by database query as

 $num_updated = db_update('node') // Table name no longer needs {}
  ->fields(array(
    'title' => "custom_title",
    'status' => 1,
  ))
  ->condition('nid', $nid, '=')
  ->execute();

The above code is updating the node title in node table, but on node display I am not seeing the updated title.

The reason I am not using node_save() to update node title is to by pass auto entity label functionality for updating node title.

5
  • may be flushing the cache after the query can help?
    – xurshid29
    Feb 14 '14 at 8:32
  • @xurshid29 I tried clearing the caches.
    – arpitr
    Feb 14 '14 at 8:54
  • I think you need to save title field also with field_attach_update and reset cache. You can also take a look at node_save function.
    – xurshid29
    Feb 14 '14 at 9:02
  • I tried with field_attach_update also
    – arpitr
    Feb 14 '14 at 9:04
  • 1
    @xurshid29 the reason field_attach_update is not working is the same, it does not update node_revision table
    – arpitr
    Feb 14 '14 at 9:18
3

If you want to bypass just one functionality, you need to go thorough all your modules, see what they are doing on node_save() and emulate it with your queries. Most common thing people forget is to update revisions, menus and clear cache. But your way is discouraged.

Instead, you should be able to configure Automatic Entity Label not to override title, if the title is set manually, like on the screenshot below:

config screenshot

If you need to deny users title generation, you can change this setting in code with variable_set() just before call to node_save(), and revert it just after.

2
  • thanks @Molot I was trying to fix this for last 3 hours :)
    – arpitr
    Feb 14 '14 at 9:03
  • 1
    @arpitr happens to the best of us :)
    – Mołot
    Feb 14 '14 at 9:03
3

You also need to update the node_revision table. You either need to create a new revision for that node or update the latest one.

1
  • 1
    @m1no you are right if the title needs to be updated with database query than we need to update the revision table title as well and this is the answer for the title of question I have, but with Molot answer I am able to fix with cleaner way, thanks :)
    – arpitr
    Feb 14 '14 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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