Loading multiple user entities using a field API field value?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
emptyvoid's picture

Hello,

So I have been spending several days reading about the entity API and the field API both provide examples on how to define an entity/field, the structure and schema, and display handlers.

Next I reviewed user_load_multiple but to my dismay it only accepts parameters that are fields in the schema definition for user entities. I can't automatically reference fields attached via the field API.

Has anyone successfully loaded multiple users based on a value in an attached field?

For example I have a user entity and I added a "birthday" field. It is a date field that appears when the user modifies their user account. How would I write a queryobject or entityfieldquery to get back all users who birthday is in November?

I don't see a clear method or combination of methods that provide the result I'm looking for. Heck you don't even need to know the answer, if you could point me toward a method or help page I'd be all smiles.

Anyone have any experience with his or pointers?

Comments

pseudocode

mikeytown2's picture

Here's my quick tip.

<?php
 
// Find all users
 
$month = 'November';
 
$query = new EntityFieldQuery();
 
$results = $query->entityCondition('entity_type', 'user')
    ->
propertyCondition('status', 1)
    ->
fieldCondition('field_birthday_month', 'value', $month, '=')
    ->
execute();
 
$uids = array();
  foreach (
$results['user'] as $uid => $user) {
   
$uids[] = $uid;
  }
 
$accounts = user_load_multiple($uids);
?>

Posts on working with dates
http://eureka.ykyuen.info/2012/06/05/drupal-7-set-a-date-fieldcondition-...
http://drupal.stackexchange.com/questions/9276/how-to-use-entityfieldque...

Well I spent 3+ hours trying

emptyvoid's picture

Well I spent 3+ hours trying to juggle the 4 different entity and APIs required to get what I want and just couldn't figure it out.

So in 10 mins I wrote a db_query to get what I needed. It was especially impossible because I needed to join the user entity both to a field value registered via the field API and a custom table for my custom module.

$result = db_query("
SELECT

                cp.*,

                    fes.field_cuid_value

                FROM

                    users AS cp

                    INNER JOIN field_data_field_cuid AS fes ON cp.uid = fes.entity_id

                    LEFT JOIN custom_customer_queue AS cepq ON cp.uid = cepq.pid

                WHERE

                    fes.field_cuid_value IS NOT NULL

                AND

                    cepq.pid IS NULL
");

foreach($result AS $result_item) {
    // Do useful things.
}

Robert Foley Jr
Solutions Architect
http://www.robertfoleyjr.com

EntityFieldQuery honors

calebtr's picture

EntityFieldQuery honors access rules, and it can return null results if the user running the query doesn't have permission to see the field.

To get around this, you can add:

->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')

To mikeytown2's query before the ->execute(). There is also an alternative, to run the query as user 1, something like:

  $query->addMetaData('account', user_load(1));

I prefer the all-caps "DANGEROUS" flag.

I use a lot of straight db_query's also. It usually beats getting an array of nids and then node_loading them all to get at one field. There is a way that EntityFieldQuery can do this, I think, but I haven't mastered it.

But! If I want to make sure the user has access to see what I'm looking for, EntityFieldQuery is the way to go. I also have the vague sense that EntityFieldQuery is doing some caching, and that object-oriented code can be faster, both of which are usually useful.

Portland (Oregon)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week