Skip to content

Commit 3f7c56c

Browse files
ACPT-1194-part2 Resetting state in more Collection classes
People shouldn't be dependency injecting collection classes as if they were service classes. But they do anyways! So, we have to reset them. :-(
1 parent 340c7ea commit 3f7c56c

File tree

7 files changed

+93
-8
lines changed

7 files changed

+93
-8
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Selection/Collection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public function __construct(
128128
$metadataPool,
129129
$tableMaintainer
130130
);
131-
132131
$this->stockItem = $stockItem
133132
?? ObjectManager::getInstance()->get(\Magento\CatalogInventory\Model\ResourceModel\Stock\Item::class);
134133
}
@@ -145,6 +144,17 @@ protected function _construct()
145144
$this->_selectionTable = $this->getTable('catalog_product_bundle_selection');
146145
}
147146

147+
/**
148+
* @inheritDoc
149+
*/
150+
public function _resetState(): void
151+
{
152+
parent::_resetState();
153+
$this->itemPrototype = null;
154+
$this->catalogRuleProcessor = null;
155+
$this->websiteScopePriceJoined = false;
156+
}
157+
148158
/**
149159
* Set store id for each collection item when collection was loaded.
150160
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod

app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ protected function _construct()
137137
$this->_init(Category::class, \Magento\Catalog\Model\ResourceModel\Category::class);
138138
}
139139

140+
/**
141+
* @inheritDoc
142+
*/
143+
public function _resetState(): void
144+
{
145+
parent::_resetState();
146+
$this->_productTable = null;
147+
$this->_productStoreId = null;
148+
$this->_productWebsiteTable = null;
149+
$this->_loadWithProductCount = false;
150+
}
151+
140152
/**
141153
* Add Id filter
142154
*

app/code/Magento/Catalog/Model/ResourceModel/Collection/AbstractCollection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public function __construct(
7676
);
7777
}
7878

79+
/**
80+
* @inheritDoc
81+
*/
82+
public function _resetState(): void
83+
{
84+
parent::_resetState();
85+
$this->_storeId = null;
86+
}
87+
7988
/**
8089
* Retrieve Entity Primary Key
8190
*

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
102102
*/
103103
protected $_productLimitationFilters;
104104

105+
/**
106+
* @var ProductLimitationFactory
107+
*/
108+
private $productLimitationFactory;
109+
105110
/**
106111
* Category product count select
107112
*
@@ -354,10 +359,10 @@ public function __construct(
354359
$this->_resourceHelper = $resourceHelper;
355360
$this->dateTime = $dateTime;
356361
$this->_groupManagement = $groupManagement;
357-
$productLimitationFactory = $productLimitationFactory ?: ObjectManager::getInstance()->get(
362+
$this->productLimitationFactory = $productLimitationFactory ?: ObjectManager::getInstance()->get(
358363
\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory::class
359364
);
360-
$this->_productLimitationFilters = $productLimitationFactory->create();
365+
$this->_productLimitationFilters = $this->productLimitationFactory->create();
361366
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
362367
parent::__construct(
363368
$entityFactory,
@@ -387,6 +392,36 @@ public function __construct(
387392
->get(Gallery::class);
388393
}
389394

395+
/**
396+
* @inheritDoc
397+
*/
398+
public function _resetState(): void
399+
{
400+
parent::_resetState();
401+
$this->_flatEnabled = [];
402+
$this->_initTables();
403+
$this->_addUrlRewrite = false;
404+
$this->_urlRewriteCategory = '';
405+
$this->_addFinalPrice = false;
406+
$this->_allIdsCache = null;
407+
$this->_addTaxPercents = false;
408+
$this->_productLimitationFilters = $this->productLimitationFactory->create();
409+
$this->_productCountSelect = null;
410+
$this->_isWebsiteFilter = false;
411+
$this->_priceDataFieldFilters = [];
412+
$this->_priceExpression = null;
413+
$this->_additionalPriceExpression = null;
414+
$this->_maxPrice = null;
415+
$this->_minPrice = null;
416+
$this->_priceStandardDeviation = null;
417+
$this->_pricesCount = null;
418+
$this->_catalogPreparePriceSelect = null;
419+
$this->needToAddWebsiteNamesToResult = null;
420+
$this->linkField = null;
421+
$this->backend = null;
422+
$this->emptyItem = null;
423+
}
424+
390425
/**
391426
* Get cloned Select after dispatching 'catalog_prepare_price_select' event
392427
*

app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ protected function _construct()
180180
{
181181
}
182182

183+
/**
184+
* @inheritDoc
185+
*/
186+
public function _resetState(): void
187+
{
188+
parent::_resetState();
189+
$this->_itemsById = [];
190+
$this->_staticFields = [];
191+
$this->_entity = null;
192+
$this->_reset();
193+
}
194+
183195
/**
184196
* Retrieve table name
185197
*
@@ -1597,14 +1609,12 @@ protected function _afterLoad()
15971609
protected function _reset()
15981610
{
15991611
parent::_reset();
1600-
16011612
$this->_selectEntityTypes = [];
16021613
$this->_selectAttributes = [];
16031614
$this->_filterAttributes = [];
16041615
$this->_joinEntities = [];
16051616
$this->_joinAttributes = [];
16061617
$this->_joinFields = [];
1607-
16081618
return $this;
16091619
}
16101620

app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ protected function _construct()
159159
$this->_initTables();
160160
}
161161

162+
/**
163+
* @inheritDoc
164+
*/
165+
public function _resetState(): void
166+
{
167+
parent::_resetState();
168+
$this->_entitiesAlias = [];
169+
$this->_addStoreDataFlag = false;
170+
$this->_storesIds = [];
171+
}
172+
162173
/**
163174
* Initialize select
164175
*

lib/internal/Magento/Framework/Data/Collection/AbstractDb.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,12 @@ public function __construct(
125125
public function _resetState(): void
126126
{
127127
parent::_resetState();
128-
$this->setConnection($this->_conn);
128+
$this->_reset();
129129
$this->_idFieldName = null;
130130
$this->_bindParams = [];
131-
$this->_data = null;
132131
$this->map = null;
133132
$this->_fetchStmt = null;
134133
$this->_isOrdersRendered = false;
135-
$this->extensionAttributesJoinProcessor = null;
136134
}
137135

138136
/**

0 commit comments

Comments
 (0)