feat: Add the ability to .select()
custom fields by their field handle
#11790
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Currently, if you want to do something like:
...where
plainText
is a custom field handle, it won't work, because field columns in the content table have a prefix, and sometimes a suffix as well.This makes it nigh impossible to do the above code from Twig, because you need to call
ElementHelper::fieldColumnFromField()
on the field handle to get the column, andElementHelper
isn't directly available in Twig.This makes using
.select()
for optimal queries somewhat limited via Twig, as often what you care about is custom fields.This PR leverages the existing
yii\db\Query::normalizeSelect()
method to swap in the real column name for custom fields (after Yii2 has already normalized them to an array), so the above "just works" and returns something like this:It'll also "just work" via PHP as well. It only affects
ElementQuery
's, and only when.select()
is used, so it should be pretty unintrusive.