Skip to content

Commit 1748694

Browse files
authored
Merge pull request #9755 from craftcms/feature/dev-65-integrate-collections-library
Collections
2 parents 1fb3d80 + 3a39db7 commit 1748694

11 files changed

+249
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Added `craft\db\Migration::dropForeignKeyIfExists()`.
2323
- Added `craft\db\Migration::dropIndexIfExists()`.
2424
- Added `craft\db\Migration::renameTable()`.
25+
- Added `craft\db\Query::collect()`, which returns the query results as an `Illuminate\Support\Collection` object rather than an array. ([#8513](https://github.com/craftcms/cms/discussions/8513))
2526
- Added `craft\db\Table::ASSETINDEXINGSESSIONS`.
2627
- Added `craft\elements\Asset::setFilename()`.
2728
- Added `craft\elements\User::$active`.
@@ -59,6 +60,7 @@
5960
- Added `craft\services\Users::ensureUserByEmail()`, which will return a user for the given email, creating one if it didn’t exist yet.
6061
- Added `craft\services\Users::EVENT_AFTER_DEACTIVATE_USER`.
6162
- Added `craft\services\Users::EVENT_BEFORE_DEACTIVATE_USER`.
63+
- Added the Illuminate Collections package. ([#8475](https://github.com/craftcms/cms/discussions/8475))
6264

6365
### Changed
6466
- Craft now requires PHP 7.4 or later.
@@ -134,6 +136,7 @@
134136
- Widgets’ `getTitle()` methods can now have a `?string` return type declaration.
135137
- Widgets’ `icon()` methods must now have a `?string` return type declaration.
136138
- Widgets’ `maxColspan()` methods must now have an `?int` return type declaration.
139+
- `craft\base\ElementInterface::getEagerLoadedElements()` now returns an `Illuminate\Support\Collection` object instead of an array. ([#8513](https://github.com/craftcms/cms/discussions/8513))
137140
- `craft\base\MemoizableArray` no longer extends `ArrayObject`, and now implements `IteratorAggregate` and `Countable` directly.
138141
- `craft\base\Model::datetimeAttributes()` is now called from the constructor, instead of the `init()` method.
139142
- `craft\base\Model::setAttributes()` now normalizes date attributes into `DateTime` objects.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"elvanto/litemoji": "^3.0.1",
4242
"enshrined/svg-sanitize": "~0.14.0",
4343
"guzzlehttp/guzzle": "^7.2.0",
44+
"illuminate/collections": "^8.55",
4445
"league/oauth2-client": "^2.6.0",
4546
"mikehaertl/php-shellcommand": "^1.6.3",
4647
"pixelandtonic/imagine": "~1.2.4.1",

composer.lock

Lines changed: 200 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/base/Element.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use craft\validators\StringValidator;
6060
use craft\web\UploadedFile;
6161
use DateTime;
62+
use Illuminate\Support\Collection;
6263
use Twig\Markup;
6364
use yii\base\ErrorHandler;
6465
use yii\base\Event;
@@ -1546,7 +1547,7 @@ private static function _indexOrderByColumns(string $sourceKey, array $viewState
15461547
private $_nextSibling;
15471548

15481549
/**
1549-
* @var ElementInterface[][]
1550+
* @var Collection[]
15501551
*/
15511552
private array $_eagerLoadedElements = [];
15521553

@@ -3394,7 +3395,7 @@ public function hasEagerLoadedElements(string $handle): bool
33943395
/**
33953396
* @inheritdoc
33963397
*/
3397-
public function getEagerLoadedElements(string $handle): ?array
3398+
public function getEagerLoadedElements(string $handle): ?Collection
33983399
{
33993400
if (!isset($this->_eagerLoadedElements[$handle])) {
34003401
return null;
@@ -3438,7 +3439,7 @@ public function setEagerLoadedElements(string $handle, array $elements): void
34383439
$this->trigger(self::EVENT_SET_EAGER_LOADED_ELEMENTS, $event);
34393440
if (!$event->handled) {
34403441
// No takers. Just store it in the internal array then.
3441-
$this->_eagerLoadedElements[$handle] = $elements;
3442+
$this->_eagerLoadedElements[$handle] = new Collection($elements);
34423443
}
34433444
}
34443445
}

src/base/ElementInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use craft\errors\InvalidFieldException;
1212
use craft\models\FieldLayout;
1313
use craft\models\Site;
14+
use Illuminate\Support\Collection;
1415
use Twig\Markup;
1516

1617

@@ -1283,9 +1284,9 @@ public function hasEagerLoadedElements(string $handle): bool;
12831284
* Returns the eager-loaded elements for a given handle.
12841285
*
12851286
* @param string $handle The handle of the eager-loaded elements
1286-
* @return ElementInterface[]|null The eager-loaded elements, or null if they hadn't been eager-loaded
1287+
* @return Collection|null The eager-loaded elements, or null if they hadn't been eager-loaded
12871288
*/
1288-
public function getEagerLoadedElements(string $handle): ?array;
1289+
public function getEagerLoadedElements(string $handle): ?Collection;
12891290

12901291
/**
12911292
* Sets some eager-loaded elements on a given handle.

src/db/Query.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use craft\base\ClonefixTrait;
1111
use craft\events\DefineBehaviorsEvent;
1212
use craft\helpers\ArrayHelper;
13+
use Illuminate\Support\Collection;
1314
use yii\base\Exception;
1415
use yii\base\Model;
1516
use yii\db\Connection as YiiConnection;
@@ -156,6 +157,19 @@ public function all($db = null): array
156157
}
157158
}
158159

160+
/**
161+
* Executes the query and returns all results as a collection.
162+
*
163+
* @param YiiConnection|null $db The database connection used to generate the SQL statement.
164+
* If this parameter is not given, the `db` application component will be used.
165+
* @return Collection A collection of the resulting elements.
166+
* @since 4.0.0
167+
*/
168+
public function collect(?YiiConnection $db = null): Collection
169+
{
170+
return new Collection($this->all($db));
171+
}
172+
159173
/**
160174
* @inheritdoc
161175
* @return array|Model|null first row of the query result array, or `null` if there are no query results.

src/elements/MatrixBlock.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use craft\models\MatrixBlockType as MatrixBlockTypeModel;
2323
use craft\records\MatrixBlock as MatrixBlockRecord;
2424
use craft\web\assets\matrix\MatrixAsset;
25+
use Illuminate\Support\Collection;
2526
use yii\base\Exception;
2627
use yii\base\InvalidConfigException;
2728

@@ -373,7 +374,7 @@ public function hasEagerLoadedElements(string $handle): bool
373374
/**
374375
* @inheritdoc
375376
*/
376-
public function getEagerLoadedElements(string $handle): ?array
377+
public function getEagerLoadedElements(string $handle): ?Collection
377378
{
378379
// See if we have this stored with a block type-specific handle
379380
$blockTypeHandle = $this->getType()->handle . ':' . $handle;

src/elements/db/ElementQueryInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use craft\db\Query;
1414
use craft\models\Site;
1515
use craft\search\SearchQuery;
16+
use Illuminate\Support\Collection;
1617
use IteratorAggregate;
1718
use yii\base\Arrayable;
1819
use yii\db\Connection;
@@ -1434,6 +1435,16 @@ public function positionedAfter($value): self;
14341435
*/
14351436
public function all($db = null): array;
14361437

1438+
/**
1439+
* Executes the query and returns all results as a collection.
1440+
*
1441+
* @param Connection|null $db The database connection used to generate the SQL statement.
1442+
* If this parameter is not given, the `db` application component will be used.
1443+
* @return Collection A collection of the resulting elements.
1444+
* @since 4.0.0
1445+
*/
1446+
public function collect(?Connection $db = null): Collection;
1447+
14371448
/**
14381449
* Executes the query and returns a single row of result.
14391450
*

src/fields/Assets.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use craft\services\Gql as GqlService;
3434
use craft\web\UploadedFile;
3535
use GraphQL\Type\Definition\Type;
36+
use Illuminate\Support\Collection;
3637
use yii\base\InvalidConfigException;
3738

3839
/**
@@ -486,9 +487,9 @@ public function getContentGqlType()
486487
/**
487488
* @inheritdoc
488489
*/
489-
protected function tableAttributeHtml(array $elements): string
490+
protected function tableAttributeHtml(Collection $elements): string
490491
{
491-
return Cp::elementPreviewHtml($elements, Cp::ELEMENT_SIZE_SMALL, false, true, $this->previewMode === self::PREVIEW_MODE_FULL);
492+
return Cp::elementPreviewHtml($elements->all(), Cp::ELEMENT_SIZE_SMALL, false, true, $this->previewMode === self::PREVIEW_MODE_FULL);
492493
}
493494

494495
// Events

0 commit comments

Comments
 (0)