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

[5.x]: Error in advanced element query "select" when adding column alias on custom field #15827

Closed
danbrellis opened this issue Oct 2, 2024 · 2 comments
Assignees
Labels

Comments

@danbrellis
Copy link

What happened?

Description

When trying to do an advanced element query calling a custom field in the select statement, I get an SQL error if I give the custom field a column alias.

This works

{% set critters = craft.entries()
  .section('critters')
  .orderBy('title')
  .limit(null)
  .select(['title as t', 'scientificName'])
  .asArray()
  .all() %}

This doesn't work

{% set critters = craft.entries()
  .section('critters')
  .orderBy('title')
  .limit(null)
  .select(['title as t', 'scientificName as l'])
  .asArray()
  .all() %}

Note the as l for the scientificName custom field.

Expected behavior

An array of objects with keys t and l

Actual behavior

SQL error: Column not found: 1054 Unknown column 'scientificName' in 'field list'.

Craft CMS version

5.4.6

PHP version

8.3.10

Operating system and version

No response

Database type and version

mysql 8.0

Image driver and version

No response

Installed plugins and versions

No response

@brandonkelly
Copy link
Member

This is currently expected, as aliased columns are excluded from being mapped to the actual custom field value expressions:

// Is this a mapped column name (without a custom alias)?
if ($alias === $column && isset($this->_columnMap[$alias])) {

That said, I can’t find any reason for that limitation, and it seems to be just erring on the safe side, so I’ve removed that check for Craft 4.13 and 5.5. (39bd164)

@brandonkelly
Copy link
Member

Craft 4.13.0 and 5.5.0 are out now with that change 🎉

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

No branches or pull requests

3 participants