Problem/Motivation

When making an entity query on an entity type with the address field if you try to filter on the address field existing an sql error results similar to the following.

SQLSTATE[42S22]: Column not found: 1054 Unknown column  
   'user__field_user_address.field_user_address_' in 'where cl  
  ause': SELECT "base_table"."uid" AS "uid&quot  
  ;, "base_table"."uid" AS "base_table_uid"      
  FROM                                                                         
  "users" "base_table"                                     
  INNER JOIN "user__field_user_address" "user__field_user_addr  
  ess" ON "user__field_user_address"."entity_id" = &  
  quot;base_table"."uid"                                        
  WHERE "user__field_user_address"."field_user_address_"   
  IS NOT NULL; Array                                                           
  (                                                                            
  )      

This has impacted modules such as site_audit, fontawesome, and field_encrypt.

Steps to reproduce

- Add an address field to a bundle.
- Create an entity query for the entity type the bundle belongs to.
- Add $query->exists($field_name); to the query before running

Proposed resolution

In an issue I will mark as related the following was proposed:

In order for entityQuery to return a count of fields, a join is made against the field's table. For example, node is joined with node__address_field_name. It then assumes that there is something like a value or target_id column and concatenates this with the field name to identify a column name to check for values. Perhaps there's a way for the Address module to specify a value for entityQuery to use in this case...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

damondt created an issue. See original summary.

damondt’s picture

damondt’s picture

damondt’s picture

damondt’s picture

damondt’s picture

damondt’s picture

Issue summary: View changes
ytsurk’s picture

Should probably be "address"ed in core, see #3001496: Add an alter hook to EntityQuery

Right now we could alter the entity query with the entity api, but hopefully soon in core:
IMO this module should skip the not/Exists check, when the field definition states NULL for mainPropertyName.

This may even be applied in core's SQL?Query, but loading there the field definition seems resourcey...

damondt’s picture

To be clear for future readers, the core issue referenced above may allow for an easier/better fix to be implemented, but would not fix this issue. Thanks to @ytsurk we now know this happens when a field definition defines it's mainPropertyName as null.

gombi’s picture

Hi,
after some investigation, it seems that when using the web interface, the country_code is always set. Additionally, the module is already using the country_code field to determine if the AddressItem is empty.

The mainPropertyName was set to NULL in #2823536, but because we (should) always have at least the country_code, I think we can use that field as the mainPropertyName, which then seems to resolve the issue.

ytsurk’s picture

It'll be a solution to the issue to revert that change, and have a mainPropertyName again, but, the AddressItem (as mentioned in the linked issue #2823536: AddressItem should override FieldItemBase::mainPropertyName()) would need to always have the country_code field to be present. Is this the case?

  • jsacksick committed 8948587 on 8.x-1.x authored by gombi
    Issue #3250973 by gombi: Compatibility With Entity Query Field Exists...
jsacksick’s picture

Status: Needs review » Fixed

Looks like the right fix to me since AddressItem has the following isEmpty() method:

 /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->country_code;
    return $value === NULL || $value === '';
  }

Commited, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.