Skip to content

Commit 550037d

Browse files
committed
Add macros for deprecated/renamed functions
1 parent 96705b6 commit 550037d

File tree

6 files changed

+52
-35
lines changed

6 files changed

+52
-35
lines changed

src/Database/Queries/Concerns/OverridesResults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function setResultOverride(array $elements): void
7070
* @see getResultOverride()
7171
* @see setResultOverride()
7272
*/
73-
public function clearOverride(): void
73+
public function clearResultOverride(): void
7474
{
7575
$this->override = $this->overrideCriteria = null;
7676
}

src/Database/Queries/ElementQuery.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,6 @@ public function all(array|string $columns = ['*']): ElementCollection|array
480480
return $this->get($columns);
481481
}
482482

483-
/**
484-
* Execute the query as a "select" statement.
485-
*
486-
* @return \craft\elements\ElementCollection<int, TElement>
487-
*/
488-
public function collect(array|string $columns = ['*']): ElementCollection
489-
{
490-
$this->asArray = false;
491-
492-
return $this->get($columns);
493-
}
494-
495483
public function one(array|string $columns = ['*']): ?ElementInterface
496484
{
497485
return $this->first($columns);

src/Field/BaseRelationField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ public function getPreviewHtml(mixed $value, ElementInterface $element): string
982982
{
983983
/** @var ElementQueryInterface|ElementCollection $value */
984984
if ($value instanceof ElementQueryInterface) {
985-
$value = $this->_all($value, $element)->collect();
985+
$value = $this->_all($value, $element)->get();
986986
} else {
987987
// todo: come up with a way to get the normalized field value ignoring the eager-loaded value
988988
$rawValue = $element->getBehavior('customFields')->{$this->handle} ?? null;
@@ -1200,7 +1200,7 @@ public function getRelationTargetIds(ElementInterface $element): array
12001200
// just running $this->_all()->ids() will cause the query to get adjusted
12011201
// see https://github.com/craftcms/cms/issues/14674 for details
12021202
$targetIds = $this->_all($value, $element)
1203-
->collect()
1203+
->get()
12041204
->map(fn (ElementInterface $element) => $element->id)
12051205
->all();
12061206
}

yii2-adapter/legacy/controllers/ElementsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ private function _contextMenuItems(
791791
->status(null)
792792
->orderBy(['dateUpdated' => SORT_DESC])
793793
->with(['draftCreator'])
794-
->collect()
794+
->get()
795795
->filter(fn(ElementInterface $draft) => $elementsService->canView($draft, $user))
796796
->all();
797797
} else {

yii2-adapter/src/Yii2ServiceProvider.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
use craft\services\Utilities;
6767
use craft\utilities\AssetIndexes;
6868
use craft\utilities\ClearCaches;
69+
use craft\web\Application;
6970
use craft\web\twig\GlobalsExtension;
7071
use craft\web\twig\variables\Cp as CpVariable;
7172
use craft\web\UrlManager;
7273
use craft\web\View;
7374
use CraftCms\Aliases\Aliases;
7475
use CraftCms\Cms\Cms;
7576
use CraftCms\Cms\Config\BaseConfig;
77+
use CraftCms\Cms\Database\Queries\ElementQuery;
7678
use CraftCms\Cms\Database\Table;
7779
use CraftCms\Cms\Edition\Events\EditionChanged;
7880
use CraftCms\Cms\Field\Events\RegisterFieldTypes;
@@ -207,6 +209,33 @@ private function registerMacros(): void
207209

208210
$this->dispatchComponentEvent($name, $event);
209211
});
212+
213+
ElementQuery::macro('getCachedResult', function() {
214+
Deprecator::log('ElementQuery-getCachedResult', 'Calling ->getCachedResult on an ElementQuery is deprecated. Use ->getResultOverride() instead.');
215+
216+
/** @var ElementQuery $this */
217+
return $this->getResultOverride();
218+
});
219+
220+
ElementQuery::macro('setCachedResult', function(array $elements) {
221+
Deprecator::log('ElementQuery-setCachedResult', 'Calling ->setCachedResult on an ElementQuery is deprecated. Use ->setResultOverride() instead.');
222+
223+
/** @var ElementQuery $this */
224+
$this->setResultOverride($elements);
225+
});
226+
227+
ElementQuery::macro('clearCachedResult', function() {
228+
Deprecator::log('ElementQuery-clearCachedResult', 'Calling ->clearCachedResult on an ElementQuery is deprecated. Use ->clearResultOverride() instead.');
229+
230+
/** @var ElementQuery $this */
231+
$this->clearResultOverride();
232+
});
233+
234+
ElementQuery::macro('collect', function() {
235+
Deprecator::log('ElementQuery-collect', 'Calling ->collect on an ElementQuery is deprecated. ElementQuery now returns a collection by default.');
236+
237+
return $this->get();
238+
});
210239
}
211240

212241
protected function registerLegacyApp(): void
@@ -246,7 +275,7 @@ protected function registerLegacyApp(): void
246275
$app->setTimeZone(app()->getTimezone());
247276
$app->language = app()->getLocale();
248277

249-
\Craft::$app = $app;
278+
Craft::$app = $app;
250279

251280
$this->bootEvents();
252281
self::bootYiiEvents();
@@ -468,11 +497,11 @@ private function bootEvents(): void
468497
$craft = app('Craft');
469498

470499
// Fire an 'afterEditionChange' event
471-
if (!$craft->hasEventHandlers(\craft\web\Application::EVENT_AFTER_EDITION_CHANGE)) {
500+
if (!$craft->hasEventHandlers(Application::EVENT_AFTER_EDITION_CHANGE)) {
472501
return;
473502
}
474503

475-
$craft->trigger(\craft\web\Application::EVENT_AFTER_EDITION_CHANGE, new EditionChangeEvent([
504+
$craft->trigger(Application::EVENT_AFTER_EDITION_CHANGE, new EditionChangeEvent([
476505
'oldEdition' => $event->oldEdition->value,
477506
'newEdition' => $event->newEdition->value,
478507
]));
@@ -1152,7 +1181,7 @@ private function ensureNewMigrationTable(): void
11521181
Artisan::call('craft:migrate:migration-table', [
11531182
'--force' => true,
11541183
]);
1155-
} catch (\PDOException) {
1184+
} catch (PDOException) {
11561185
// No database connection
11571186
}
11581187
}

yii2-adapter/tests/unit/elements/ElementCollectionTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function _fixtures(): array
3333

3434
public function testFind(): void
3535
{
36-
$collection = Entry::find()->collect();
36+
$collection = Entry::find()->get();
3737
self::assertInstanceOf(ElementCollection::class, $collection);
3838
$first = $collection->first();
3939
self::assertInstanceOf(Entry::class, $first);
@@ -47,7 +47,7 @@ public function testFind(): void
4747

4848
public function testContains(): void
4949
{
50-
$collection = Entry::find()->collect();
50+
$collection = Entry::find()->get();
5151
self::assertInstanceOf(ElementCollection::class, $collection);
5252
self::assertTrue($collection->contains('title', 'Theories of life'));
5353
self::assertTrue($collection->contains(fn(Entry $entry) => $entry->title === 'Theories of life'));
@@ -63,7 +63,7 @@ public function testContains(): void
6363

6464
public function testIds(): void
6565
{
66-
$collection = Entry::find()->collect();
66+
$collection = Entry::find()->get();
6767
self::assertInstanceOf(ElementCollection::class, $collection);
6868
$ids = $collection->map(fn(Entry $entry) => $entry->id)->all();
6969
self::assertSame($ids, $collection->ids()->all());
@@ -72,7 +72,7 @@ public function testIds(): void
7272
public function testMerge(): void
7373
{
7474
/** @var ElementCollection<Entry|User> $collection */
75-
$collection = Entry::find()->collect();
75+
$collection = Entry::find()->get();
7676
self::assertInstanceOf(ElementCollection::class, $collection);
7777
$first = $collection->first();
7878
self::assertInstanceOf(Entry::class, $first);
@@ -86,7 +86,7 @@ public function testMerge(): void
8686

8787
public function testMap(): void
8888
{
89-
$collection = Entry::find()->collect();
89+
$collection = Entry::find()->get();
9090
self::assertInstanceOf(ElementCollection::class, $collection);
9191
$mapped = $collection->map(fn(Entry $entry) => new Entry());
9292
self::assertInstanceOf(ElementCollection::class, $mapped);
@@ -96,7 +96,7 @@ public function testMap(): void
9696

9797
public function testMapWithKeys(): void
9898
{
99-
$collection = Entry::find()->collect();
99+
$collection = Entry::find()->get();
100100
self::assertInstanceOf(ElementCollection::class, $collection);
101101
$mapped = $collection->mapWithKeys(fn(Entry $entry, int|string $key) => [$entry->id => new Entry()]);
102102
self::assertInstanceOf(ElementCollection::class, $mapped);
@@ -107,7 +107,7 @@ public function testMapWithKeys(): void
107107

108108
public function testFresh(): void
109109
{
110-
$collection = Entry::find()->collect();
110+
$collection = Entry::find()->get();
111111
self::assertInstanceOf(ElementCollection::class, $collection);
112112
$collection->each(function(Entry $entry) {
113113
$entry->title .= 'edit';
@@ -120,10 +120,10 @@ public function testFresh(): void
120120

121121
public function testDiff(): void
122122
{
123-
$collection1 = Entry::find()->limit(4)->collect();
123+
$collection1 = Entry::find()->limit(4)->get();
124124
self::assertInstanceOf(ElementCollection::class, $collection1);
125125
self::assertSame(4, $collection1->count());
126-
$collection2 = Entry::find()->offset(3)->collect();
126+
$collection2 = Entry::find()->offset(3)->get();
127127
self::assertInstanceOf(ElementCollection::class, $collection2);
128128
self::assertTrue($collection2->isNotEmpty());
129129
$diff = $collection1->diff($collection2->all());
@@ -132,10 +132,10 @@ public function testDiff(): void
132132

133133
public function testIntersect(): void
134134
{
135-
$collection1 = Entry::find()->limit(4)->collect();
135+
$collection1 = Entry::find()->limit(4)->get();
136136
self::assertInstanceOf(ElementCollection::class, $collection1);
137137
self::assertSame(4, $collection1->count());
138-
$collection2 = Entry::find()->offset(3)->collect();
138+
$collection2 = Entry::find()->offset(3)->get();
139139
self::assertInstanceOf(ElementCollection::class, $collection2);
140140
self::assertTrue($collection2->isNotEmpty());
141141
$intersect = $collection1->intersect($collection2->all());
@@ -144,7 +144,7 @@ public function testIntersect(): void
144144

145145
public function testUnique(): void
146146
{
147-
$collection = Entry::find()->limit(4)->collect();
147+
$collection = Entry::find()->limit(4)->get();
148148
self::assertInstanceOf(ElementCollection::class, $collection);
149149
$count = $collection->count();
150150
$collection->push(...$collection->all());
@@ -155,7 +155,7 @@ public function testUnique(): void
155155

156156
public function testOnly(): void
157157
{
158-
$collection = Entry::find()->collect();
158+
$collection = Entry::find()->get();
159159
self::assertInstanceOf(ElementCollection::class, $collection);
160160
self::assertNotEquals(1, $collection->count());
161161
$first = $collection->first();
@@ -166,7 +166,7 @@ public function testOnly(): void
166166

167167
public function testExcept(): void
168168
{
169-
$collection = Entry::find()->collect();
169+
$collection = Entry::find()->get();
170170
self::assertInstanceOf(ElementCollection::class, $collection);
171171
$count = $collection->count();
172172
$first = $collection->first();
@@ -177,7 +177,7 @@ public function testExcept(): void
177177

178178
public function testBaseMethods(): void
179179
{
180-
$collection = Entry::find()->collect();
180+
$collection = Entry::find()->get();
181181
self::assertInstanceOf(ElementCollection::class, $collection);
182182
self::assertSame(Collection::class, get_class($collection->countBy(fn(Entry $entry) => $entry->sectionId)));
183183
self::assertSame(Collection::class, get_class($collection->collapse()));

0 commit comments

Comments
 (0)