Skip to content

Collections #9755

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

Merged
merged 3 commits into from
Aug 25, 2021
Merged

Collections #9755

merged 3 commits into from
Aug 25, 2021

Conversation

brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Aug 25, 2021

Description

  • Adds the Illuminate Collections package
  • Adds craft\db\Query::collect(), which returns the query results as a Collection
  • Modifies Element::getEagerLoadedElements() to return eager-loaded elements as a Collection

As a result, templates now have a consistent way of interacting with elements, whether they were eager-loaded or queried directly: templates can just call collect() rather than all(). collect() will either end up referring to Illuminate\Support\Collection::collect() (if eager-loaded) or craft\db\Query::collect() (if not).

(Templates which are already looping over eager-loaded element arrays won’t break, because Illuminate\Support\Collection objects are traversable.)

Related issues & discussions

@linear
Copy link

linear bot commented Aug 25, 2021

@brandonkelly brandonkelly changed the title Feature/dev 65 integrate collections library Collections Aug 25, 2021
@brandonkelly brandonkelly merged commit 1748694 into 4.0 Aug 25, 2021
@brandonkelly brandonkelly deleted the feature/dev-65-integrate-collections-library branch August 25, 2021 02:06
@michaelrog
Copy link

Curiously... why not have all() return a Collection? (except when asArray() is used)

@brandonkelly
Copy link
Member Author

Because I don’t want to deviate from the yii\db\Query::all() return type.

@michaelrog
Copy link

Curiously... why not have all() return a Collection? (except when asArray() is used)

In retrospect, this was a silly question, since all() is also a Collection method that returns the underlying array... So yeah, good call.

query.all().all() would get awkward.

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.

2 participants