I'm building a module to let users apply to a job. The user goes to a job advert, presses apply and it sends their CV to the author of the job advert.

I have it working, but I'm worried that the way I grab the users email is inefficient and I was wondering if there was a better way to do it.

Current Set-up

I have a custom form with a submit function. I grab the email of the author of the current node using the following:

function jobapplication_form_submit($form, &$form_state) {
    if(arg(0) == 'node' && is_numeric(arg(1))){
            $node = node_load(arg(1));
            $user = user_load_multiple(array(),array('uid' => $node->uid));
    $email = $user['1']->mail;

Is there a way to get the node author email without having to load the entire node? It seems really inefficient (at least to my inexperienced eyes).


It is a standard issue of efficiency versus convenience. You can, of course, simply join {node} and {users} tables on uid field and do it in simple select, taking just that one field you really need. And indeed it will be faster. But you will need to rebuild that part of the code if some day one of these tables will get renamed or restructured. That's why people are using CMS in the first place - to stop worrying about low level and get the job done simple and easy. Computers are cheaper than people these days.

It is possible that you will save a second of CPU time and 30kB of RAM. So what? Do you really expect so high traffic that it'll be a problem? Databases cache their results in RAM too, so it might be actually faster, overall, to always read whole node. It means one node set to cache instead of dozen sets with different fields selected. Or might not. It depends on to many variables to know, so it seems usual to considered adjusting these variables better than complicating code.

  • Hmm ok. I did start using Drupal for that reason, then as I learn more I start worrying about efficiency. I'm not familiar with joining tables, I'll go look that up and see if I can perform some speed tests. Aug 26 '13 at 14:10
  • @DominicWoodman start with db_select, especially this join example :)
    – Mołot
    Aug 26 '13 at 14: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.