From fed4c52ecdb4b9faad97a4f67ea57316aa99bbee Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 16 Aug 2022 21:07:28 -0400 Subject: [PATCH 1/3] feat: Add the ability to `.select()` custom fields by their field handle --- src/elements/db/ElementQuery.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/elements/db/ElementQuery.php b/src/elements/db/ElementQuery.php index af25423bcb2..6e0edd2d5a6 100644 --- a/src/elements/db/ElementQuery.php +++ b/src/elements/db/ElementQuery.php @@ -1950,6 +1950,33 @@ public function getCacheTags(): array return $this->_cacheTags; } + /** + * @inerhitdoc + */ + protected function normalizeSelect($columns) + { + $select = parent::normalizeSelect($columns); + /** @var string|ElementInterface $class */ + /** @phpstan-var class-string|ElementInterface $class */ + $class = $this->elementType; + if ($class::hasContent() && isset($this->contentTable)) { + $this->customFields = $this->customFields(); + } + // Swap in the actual field column name for custom fields + if (is_array($this->customFields)) { + foreach ($this->customFields as $field) { + $handle = $field->handle; + if (!empty($select[$handle])) { + $column = ElementHelper::fieldColumnFromField($field); + unset($select[$handle]); + $select[$handle] = $column; + } + } + } + + return $select; + } + /** * Returns any cache invalidation tags that caches involving this element query should use as dependencies. * From c6f02260a3aa13846178eba4441a69046d40b611 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 16 Aug 2022 21:13:17 -0400 Subject: [PATCH 2/3] refactor: Check for null `$column` --- src/elements/db/ElementQuery.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/elements/db/ElementQuery.php b/src/elements/db/ElementQuery.php index 6e0edd2d5a6..577c58f6d81 100644 --- a/src/elements/db/ElementQuery.php +++ b/src/elements/db/ElementQuery.php @@ -1968,8 +1968,10 @@ protected function normalizeSelect($columns) $handle = $field->handle; if (!empty($select[$handle])) { $column = ElementHelper::fieldColumnFromField($field); - unset($select[$handle]); - $select[$handle] = $column; + if ($column) { + unset($select[$handle]); + $select[$handle] = $column; + } } } } From 1c5a7b651761f64d94d4b5cff9b04f12183fff84 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 17 Aug 2022 05:44:41 -0400 Subject: [PATCH 3/3] refactor: remove vestigial code --- src/elements/db/ElementQuery.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/elements/db/ElementQuery.php b/src/elements/db/ElementQuery.php index 577c58f6d81..1c98b52f160 100644 --- a/src/elements/db/ElementQuery.php +++ b/src/elements/db/ElementQuery.php @@ -1969,7 +1969,6 @@ protected function normalizeSelect($columns) if (!empty($select[$handle])) { $column = ElementHelper::fieldColumnFromField($field); if ($column) { - unset($select[$handle]); $select[$handle] = $column; } }