I have a field within the user's profile set with their payroll. What I'm trying to achieve within my module is find the uid, based on the value of a payroll field.

I've tried a few things, but I can't get it to work. What I'm currently trying is this, but it returns unknown field of user

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'user')
  ->fieldCondition('user', 'field_user_payroll', $payroll, '=');

$result = $query->execute();
$account = user_load($result);

$wrapper = entity_metadata_wrapper('user', $account);
$mimemail = $wrapper->mimemail->raw();

watchdog('Photo Import', "Mimemail: %mimemail", 
  array('%mimemail' => $mimemail), WATCHDOG_INFO);

Any help would be really appreciated. It's driving me crazy!

  • 1
    Have you read the docs for EntityFieldQuery? Your code is quite far off from what it should be, that's all
    – Clive
    Sep 3, 2013 at 12:18
  • Community docs for it here
    – Mołot
    Sep 3, 2013 at 12:21

1 Answer 1

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
  ->fieldCondition('field_user_payroll', 'value', $payroll, '=');
$results = $query->execute();

$users = user_load_multiple(array_keys($results['user']));
foreach($users as $user) {
    $wrapper = entity_metadata_wrapper('user', $account);
    $mimemail = $wrapper->mimemail->raw();

    watchdog('Photo Import', "Mimemail: %mimemail", array('%mimemail' => $mimemail), WATCHDOG_INFO);

as per EntityFieldQuery::fieldCondition(), EntityFieldQuery doc


  • 1
    That'll get him so far...what about $account = user_load($result); though? ;)
    – Clive
    Sep 3, 2013 at 12:54
  • Right, updated answer, even though I'm not sure what is intented with the mimemail part
    – SebCorbin
    Sep 3, 2013 at 13:12
  • @SebCorbin it looks vaguely like watchdog messages I set sometimes to know "now would ..... happen" if I want to avoid pointless mails during development, for example.
    – Mołot
    Sep 3, 2013 at 13:15
  • 1
    mimemail was only added because this piece of code is being called by cron, so I wanted a watchdog line to look out for. Not going to be using it for anything. Little modification and it's working. I needed to change: foreach($result['user'] as $user) { $account = user_load($user->uid); Sep 3, 2013 at 13:54
  • 1
    Note: user_load_multiple(array_keys($result['user'])) is able to load those users more efficient as it can do a single query or a single cache get multiple if you're using entitycache.
    – Berdir
    Sep 3, 2013 at 21:10

Your Answer

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

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