I've been using EntityFieldQuery to query my fields but I need the SQL 'like' operator. For this I'm querying the database manually. Question: How do I get the name of a field's table?

  • Maybe I'm misunderstanding your question, but are you saying that EntityFieldQuery does not support the like operator? I haven't done much with EFQ yet, but the code at EntityFieldQuery::addCondition looks like it should be supported. – marcvangend Jul 25 '11 at 9:18
  • What if I want to select field values? Not nodes based on field values, but the actual field values. – Capi Etheriel Nov 21 '12 at 21:46

What you need to use is the fieldCondition , which works pretty much like entityCondition and propertyCondition, only you are adding a condition for the field and not the entity or one of it's properties.

This is how you should query fields and entities.

If you wanted to get info about the how the data of a field was stored, that information is available on the field itself. So you could do

$field = field_info_field('field_name');
$storage_details = $field['storage']['details'];

What info you get depends on how the field is stored, as Drupal support multiple storage backends for fields.

| improve this answer | |
  • 2
    the storage details array is not specified anywhere. Calling _field_sql_storage_tablename and _field_sql_storage_coulmname directly is also ugly because they are specified as private method but still safer than messing with that array. – user49 Oct 19 '11 at 8:38
  • 1
    hook_update_N breaks with the code above. Use _field_sql_storage_tablename as I said. – user49 Oct 22 '11 at 22:56

There is another way for obtaining storage details - field_info_fields() function. If you need to get more than one table (data and revisions in your situation) you may use it

$fields = field_info_fields();
$field_current_tbl_name = array_keys($fields[FIELD_NAME]['storage']['details]['sql']['FIELD_LOAD_CURRENT']);
$field_revision_tbl_name = array_keys($fields[FIELD_NAME]['storage']['details]['sql']['FIELD_LOAD_REVISION']);

Don't forget that field_info_fields() - memory eater, so it is better to use field_info_field() if you need only single field data.

| improve this answer | |

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.