1

I am attempting to query a custom content type called players (machine name) in a Drupal 7 module and it seemed to be working fine up until an hour ago. Nothing has changed on my side but now the 'Select' works ( with no fieldCondition, but when I add a fieldCondition I am getting empty results.

Here is my query:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'players')
  ->fieldCondition('field_email_address', 'value', $email_address)
  ->fieldCondition('field_password', 'value', $password)
  ->range(0, 1);

$results = $query->execute();

I have verified and reverified the bundle name, each machine name for the fields, and the values being passed in, but when I dump and die the query results, they come up empty.

Any ideas?

  • Have you tried removing the , '=' from the field_email_address fieldCondition? – Mrweiner Aug 19 at 20:16
  • I tried that still didn't work – Wally Kolcz Aug 19 at 20:23
2

Assuming you're using the Email field module, the column name is email, rather than value.

If that's the case, just change that line to:

->fieldCondition('field_email_address', 'email', $email_address, '=')

You may need to make a similar change for the password field, depending on what type it is. The easiest way to check for valid column names is in the .install file of the module that provides it, within its implementation of hook_field_schema().

  • It's a custom content type with a text field called field_email_address – Wally Kolcz Aug 20 at 1:44
  • In that case your original code was fine, and the problem must simply be that the strings don’t match – Clive Aug 20 at 7:14
0

You may need to actually execute the query:

$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'players')
  ->fieldCondition('field_email_address', 'value', $email_address, '=')
  ->fieldCondition('field_password', 'value', $password)
  ->range(0, 1);

$results = $query->execute();
  • I do execute the query but the results are empty if you add the fieldCondition but not if you remove it. – Wally Kolcz Aug 20 at 1:44
  • Have you tried just one of the fieldConditions at a time to see if one of them breaks it but the other one works? Try with and without the range as well. Maybe there's an issue with your $email_address or $password variables. – Mrweiner Aug 20 at 4:08
  • It seems like a permission issue. When I am logged in as admin it works, when I am not, I cannot added the fieldConditions to the query. Any Ideas? – Wally Kolcz Aug 20 at 15:34
0

Ok, for some stupid reason you can query a whole database as an unauthenticated user but not with a where clause so you need to add ->addMetaData('account', user_load(1)) before the ->range(0,1). Hope this helps someone else.

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.