1

This question already has an answer here:

I'm using Drupal 7.
I'm trying to get all users with custom field field_id:

$users = user_load_multiple(array(), array('field_id' => '123456789'));
var_dump($users);

and I got the following error:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.field_id' in 'where clause': SELECT base.uid AS uid, base.name AS name, base.pass AS pass, base.mail AS mail, base.theme AS theme, base.signature AS signature, base.signature_format AS signature_format, base.created AS created, base.access AS access, base.login AS login, base.status AS status, base.timezone AS timezone, base.language AS language, base.picture AS picture, base.init AS init, base.data AS data FROM {users} base WHERE (base.field_id = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => 123456789 ) in DrupalDefaultEntityController->load() (line 191 of C:\wamp\www\my_proj\includes\entity.inc).

My questions:
1. How can get users by custom field ?
2. How to query the field type ? (is it text, int, float, etc..).

marked as duplicate by Mołot, Bala, Free Radical, AjitS, Letharion Dec 25 '13 at 15:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

You'll need an EntityFieldQuery for that:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
  ->fieldCondition('field_id', 'value', $field_id);

There's no native way to query by type of field. To do that you'll need to use the EntityFieldQuery::addTag() method to add a custom tag to the query, and then use hook_query_TAG_alter() to manually join the field_config table into the query before it's run.

  • 1
    Looks like you and this guy use similar source for code snippets ;) – Mołot Dec 19 '13 at 15:41
  • I think he was asking because of $conditions on user_load_multiplel, but under close inspection the API says the $conditions can only be on the {users} table. :) – tenken Dec 19 '13 at 15:47
  • @tenken Yeah $conditions it's a properties-only thing; it's also deprecated – Clive Dec 19 '13 at 15:48
  • 1
    @Mołot That would be 'brain' :) – Clive Dec 19 '13 at 16:02
  • @Clive I mean to get back the field type as I set it, for example field_id as text and also native types like email. – NickF Dec 19 '13 at 16:17

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