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"
;, "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...
Comment | File | Size | Author |
---|---|---|---|
#10 | address-compatibility-with-exists-method-3250973-10.patch | 451 bytes | gombi |
|
Comments
Comment #2
damondt CreditAttribution: damondt commentedComment #3
damondt CreditAttribution: damondt commentedComment #4
damondt CreditAttribution: damondt commentedComment #5
damondt CreditAttribution: damondt commentedComment #6
damondt CreditAttribution: damondt commentedComment #7
damondt CreditAttribution: damondt commentedComment #8
ytsurkShould 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...
Comment #9
damondt CreditAttribution: damondt commentedTo 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.
Comment #10
gombi CreditAttribution: gombi at Agiledrop - Your Trusted Drupal Teammates commentedHi,
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.
Comment #11
ytsurkIt'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?
Comment #13
jsacksick CreditAttribution: jsacksick at Centarro commentedLooks like the right fix to me since AddressItem has the following
isEmpty()
method:Commited, thanks!