Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the ability to .select() custom fields by their field handle #11790

Closed

Conversation

khalwat
Copy link
Contributor

@khalwat khalwat commented Aug 17, 2022

Description

Currently, if you want to do something like:

    {% set result = craft.entries.section('blog').select(['plainText']).asArray().all() %}

...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, and ElementHelper 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:

[
    0 => [
        'plainText' => 'Some plain text'
    ]
]

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.

@khalwat khalwat requested a review from a team as a code owner August 17, 2022 01:12
@brandonkelly
Copy link
Member

Implemented the same feature but in a different way via #11800, for Craft 4.3 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants