1

I have node type called online_users. The node type has a field called field_website_id. I would like to get the nodes that has a specific field_website_id.

I have try to make the following action but gets an error:

function get_list_of_users($site_id) {
$query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'users_online')
  ->propertyCondition('field_website_id', $site_id)
  ->execute();
  if (!empty($entities['node'])) {
    $node = node_load(array_shift(array_keys($entities['node'])));
  }
} 

The error:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node.field_website_id' in 'where clause': SELECT node.nid AS entity_id, node.vid AS revision_id, node.type AS bundle, :entity_type AS entity_type FROM {node} node WHERE (node.type = :db_condition_placeholder_0) AND (node.field_website_id IS NULL ) ; Array ( [:db_condition_placeholder_0] => users_online [:entity_type] => node ) in EntityFieldQuery->execute() (line 1140 of /Applications/MAMP/htdocs/Drupal/includes/entity.inc).

What is the right way to do this?

1 Answer 1

2

To filter by fields you should use fieldCondition() method instead of propertyCondition()

function get_list_of_users($site_id) { 
  $query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'node') 
    ->propertyCondition('type', 'users_online') 
    ->fieldCondition('field_website_id', $site_id) 
    ->execute(); 
  if (!empty($entities['node'])) {
    $node = node_load(array_shift(array_keys($entities['node'])));
  }
}
1
  • Does this function has cache problems? It seems that it bringing a strange results... Dubble and doesn't bring all of the results it supposed to bring (i had compared with the db query)
    – Dori
    Commented Oct 27, 2013 at 4:06

Your Answer

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

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