-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Move result projection onto Table (Table::projectAs/list); drop SelectQuery::reshape() #19484
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
Draft
dereuromark
wants to merge
2
commits into
5.next
Choose a base branch
from
find-honesty-5next
base: 5.next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1568,6 +1568,89 @@ public function findThreaded( | |
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Project query results into DTO instances and return them as a list. | ||
| * | ||
| * Terminal repository counterpart to the fluent | ||
| * {@see \Cake\ORM\Query\SelectQuery::projectAs()}. The honest return type | ||
| * (`list<T>`) lives here on the repository instead of being a generic that | ||
| * the chainable query cannot truthfully carry. Stack finders on `$query` | ||
| * first when needed, then hand it here to execute the projection. | ||
| * | ||
| * Rows are mapped from the raw selected columns (the same projection path | ||
| * as the fluent `SelectQuery::projectAs()`), so entity visibility and | ||
| * accessors do not affect the DTO data. | ||
| * | ||
| * ``` | ||
| * $dtos = $articles->projectAs(ArticleDto::class); | ||
| * | ||
| * $dtos = $articles->projectAs( | ||
| * ArticleDto::class, | ||
| * $articles->find()->where(['published' => true])->contain('Authors'), | ||
| * ); | ||
| * ``` | ||
| * | ||
| * @template T of object | ||
| * @param class-string<T> $class The DTO class to map each row into. | ||
| * @param \Cake\ORM\Query\SelectQuery<\Cake\Datasource\EntityInterface>|null $query | ||
| * A pre-built query, or null to start from `find()`. | ||
| * @return list<T> | ||
| * @since 5.4.0 | ||
| */ | ||
| public function projectAs(string $class, ?SelectQuery $query = null): array | ||
| { | ||
| $query ??= $this->find(); | ||
|
|
||
| $results = []; | ||
| foreach ($query->projectAs($class)->all() as $dto) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this method deprecated? |
||
| if ($dto instanceof $class) { | ||
| $results[] = $dto; | ||
| } | ||
| } | ||
|
|
||
| return $results; | ||
| } | ||
|
|
||
| /** | ||
| * Execute a key/value list query and return the resulting array. | ||
| * | ||
| * Terminal repository counterpart to the `list` finder | ||
| * ({@see \Cake\ORM\Table::findList()}). Returns the combined array directly | ||
| * instead of a query whose static type still claims to be a collection of | ||
| * entities. Build and stack finders on `$query` first when needed. | ||
| * | ||
| * ``` | ||
| * $options = $articles->list(); | ||
| * | ||
| * $options = $articles->list( | ||
| * $articles->find()->where(['published' => true]), | ||
| * valueField: 'title', | ||
| * ); | ||
| * ``` | ||
| * | ||
| * @param \Cake\ORM\Query\SelectQuery<TEntity|array>|null $query | ||
| * A pre-built query, or null to start from `find()`. | ||
| * @param \Closure|array<string>|string|null $keyField The path to the key field, defaults to the primary key. | ||
| * @param \Closure|array<string>|string|null $valueField The path to the value field, defaults to the display field. | ||
| * @param \Closure|array<string>|string|null $groupField The path to the field to group on. | ||
| * @param string $valueSeparator The separator used to join composite value fields. | ||
| * @return array<array-key, mixed> | ||
| * @since 5.4.0 | ||
| */ | ||
| public function list( | ||
| ?SelectQuery $query = null, | ||
| Closure|array|string|null $keyField = null, | ||
| Closure|array|string|null $valueField = null, | ||
| Closure|array|string|null $groupField = null, | ||
| string $valueSeparator = ' ', | ||
| ): array { | ||
| $query ??= $this->find(); | ||
|
|
||
| return $this->findList($query, $keyField, $valueField, $groupField, $valueSeparator) | ||
| ->all() | ||
| ->toArray(); | ||
| } | ||
|
|
||
| /** | ||
| * Out of an options array, check if the keys described in `$keys` are arrays | ||
| * and change the values for closures that will concatenate the each of the | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunate that we're churning a new method. Did we miss something during the design of the feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we did the Query part, which now looks fine.
But we didnt tackle the even more problematic formatResults() and result formatting etc, which literally is "mixed" at this point, due to the way we inject them before we execute.
The more correct approach would be to separate those concerns, always return either entity collections or array, and then pass those to the processing that are return type safe then.
I had hoped people would help the last 7 weeks to find a way to way here forward that would minimize the fallout while keeping it clean moving forward.