Skip to content
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

EAV Config Cache #2993

Merged
merged 30 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
74f9126
use eav/config singleton for caching entity types and attributes, rep…
davidhiendl Jan 24, 2023
11383b7
Add @DavidHiendl as a contributor
davidhiendl Jan 24, 2023
8cafff3
fix format inconsistencies, fixed eav/config clear fields
davidhiendl Jan 24, 2023
c22d333
fix fallback for getEntityAttributeCodes missing store_id in lookup
davidhiendl Jan 24, 2023
c6372af
Merge remote-tracking branch 'origin/20.0' into eav_config_cache
davidhiendl Jan 25, 2023
46a7c62
patch potential error with leftover option data for deleted attributes
davidhiendl Jan 25, 2023
185fbf9
optimzied/fixed calls to Varien_Profiler
davidhiendl Jan 25, 2023
50ee31d
improved phpdoc, removed redundant checks
davidhiendl Jan 25, 2023
68527f6
fixed contributor profile link
davidhiendl Jan 25, 2023
0cd0aa2
fix for eav/config->getAttribute no longer returning an empty model f…
davidhiendl Jan 26, 2023
973b447
deprecated importAttributesData, as it no longer has any purpose (all…
davidhiendl Jan 26, 2023
b0883ed
fixed one more bug related to deleted attributes when there is leftov…
davidhiendl Jan 26, 2023
fa31e60
added map lookup for entity type by code, reduce calls to app()->getS…
davidhiendl Jan 27, 2023
af111e3
fixed cache being written every request
davidhiendl Jan 27, 2023
cc7bce4
PHPCS
fballiano Jan 29, 2023
51824a9
PHPCS
fballiano Jan 29, 2023
f3027e1
fixed phpstan issues and improved phpdoc
davidhiendl Jan 29, 2023
b08c46f
Merge branch '20.0' into eav_config_cache
fballiano Jan 29, 2023
c6af34f
removed phpstan ignored patterns as those no longer apply
davidhiendl Jan 29, 2023
e02d945
Merge remote-tracking branch 'origin/20.0' into eav_config_cache
davidhiendl Feb 1, 2023
75be655
avoid Varien_Object::__call for Mage_Catalog_Model_Resource_Eav_Attri…
davidhiendl Feb 1, 2023
0baa7fa
code cleanup, hide internal exception from user
davidhiendl Feb 2, 2023
3fb665f
fix Collection::getData() returning all column values as string by co…
davidhiendl Feb 3, 2023
d7371a7
fix more phpdoc issues
davidhiendl Feb 3, 2023
eeded1d
fetch entity type via constant, add phpdoc type for Mage_Eav_Model_En…
davidhiendl Feb 9, 2023
bcef745
allow repeated calls to Mage_Eav_Model_Resource_Entity_Attribute_Coll…
davidhiendl Feb 9, 2023
ec5236c
Merge remote-tracking branch 'origin/20.0' into eav_config_cache
davidhiendl Feb 9, 2023
f77d4a8
remove commented code, change quote style
davidhiendl Feb 16, 2023
6408cf1
add note to README.md for openmage 19.x to 20.x mentioning Mage_Eav_M…
davidhiendl Feb 16, 2023
1d81d05
Merge branch '20.0' into eav_config_cache
fballiano Feb 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,15 @@
"contributions": [
"code"
]
},
{
"login": "davidhiendl",
"name": "David Hiendl",
"avatar_url": "https://avatars.githubusercontent.com/u/11006964?v=4",
"profile": "https://gitlab.com/davidhiendl",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<a href="#contributors-"><img src="https://img.shields.io/badge/all_contributors-151-orange.svg" alt="All Contributors"></a>
<a href="#contributors-"><img src="https://img.shields.io/badge/all_contributors-152-orange.svg" alt="All Contributors"></a>
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<a href="https://packagist.org/packages/openmage/magento-lts"><img src="https://poser.pugx.org/openmage/magento-lts/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/openmage/magento-lts"><img src="https://poser.pugx.org/openmage/magento-lts/license.svg" alt="License"></a>
Expand Down Expand Up @@ -505,6 +505,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/discountscott"><img src="https://avatars.githubusercontent.com/u/5454596?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>Scott Moore</b></sub></a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/rfeese"><img src="https://avatars.githubusercontent.com/u/7074181?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>Roger Feese</b></sub></a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/AGelzer"><img src="https://avatars.githubusercontent.com/u/34437931?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>Alexander Gelzer</b></sub></a></td>
<td align="center" valign="top" width="14.28%"><a href="https://gitlab.com/davidhiendl"><img src="https://avatars.githubusercontent.com/u/11006964?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>David Hiendl</b></sub></a></td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ public function getIsCurrentSetDefault()
{
$isDefault = $this->getData('is_current_set_default');
if (is_null($isDefault)) {
$defaultSetId = Mage::getModel('eav/entity_type')
->load(Mage::registry('entityType'))
$defaultSetId = Mage::getSingleton('eav/config')
->getEntityType(Mage::registry('entityType'))
->getDefaultAttributeSetId();
$isDefault = $this->_getSetId() == $defaultSetId;
$this->setData('is_current_set_default', $isDefault);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ protected function _getSetId()
{
return ((int) $this->getRequest()->getParam('id') > 0)
? (int) $this->getRequest()->getParam('id')
: Mage::getModel('eav/entity_type')
->load(Mage::registry('entityType'))
: Mage::getSingleton('eav/config')->getEntityType(Mage::registry('entityType'))
->getDefaultAttributeSetId();
}
}
2 changes: 1 addition & 1 deletion app/code/core/Mage/Api2/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ public function getEavAttributes($onlyVisible = false, $excludeSystem = false)
$model = $this->getConfig()->getResourceWorkingModel($this->getResourceType());

/** @var Mage_Eav_Model_Entity_Type $entityType */
$entityType = Mage::getModel('eav/entity_type')->load($model, 'entity_model');
$entityType = Mage::getSingleton('eav/config')->getEntityType($model, 'entity_model');
kiatng marked this conversation as resolved.
Show resolved Hide resolved

/** @var Mage_Eav_Model_Entity_Attribute $attribute */
foreach ($entityType->getAttributeCollection() as $attribute) {
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Catalog/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ public function canApplyMsrpToProductType($product)
{
if ($this->_mapApplyToProductType === null) {
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
$this->_mapApplyToProductType = $attribute->getApplyTo();
}
return empty($this->_mapApplyToProductType) || in_array($product->getTypeId(), $this->_mapApplyToProductType);
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/Model/Api2/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getAvailableAttributes($userType, $operation)
{
$attributes = $this->getAvailableAttributesFromConfig();
/** @var Mage_Eav_Model_Entity_Type $entityType */
$entityType = Mage::getModel('eav/entity_type')->loadByCode('catalog_product');
$entityType = Mage::getSingleton('eav/config')->getEntityType('catalog_product');
davidhiendl marked this conversation as resolved.
Show resolved Hide resolved
$entityOnlyAttrs = $this->getEntityOnlyAttributes($userType, $operation);
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
foreach ($entityType->getAttributeCollection() as $attribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function isValidData(array $data)
try {
$this->_validateProductType($data);
/** @var Mage_Eav_Model_Entity_Type $productEntity */
$productEntity = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY);
$productEntity = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY);
$this->_validateAttributeSet($data, $productEntity);
$this->_validateSku($data);
$this->_validateGiftOptions($data);
Expand Down
31 changes: 12 additions & 19 deletions app/code/core/Mage/Catalog/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,13 @@ protected function _getResource()
public function getAttributesUsedInProductListing()
{
if (is_null($this->_usedInProductListing)) {
$allAttributes = Mage::getSingleton('eav/config')
->getAttributes(Mage_Catalog_Model_Product::ENTITY);
$this->_usedInProductListing = [];
$entityType = Mage_Catalog_Model_Product::ENTITY;
$attributesData = $this->_getResource()
->setStoreId($this->getStoreId())
->getAttributesUsedInListing();
Mage::getSingleton('eav/config')
->importAttributesData($entityType, $attributesData);
foreach ($attributesData as $attributeData) {
$attributeCode = $attributeData['attribute_code'];
$this->_usedInProductListing[$attributeCode] = Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode);
foreach ($allAttributes as $attribute) {
if ($attribute->getData('used_in_product_listing')) {
$this->_usedInProductListing[$attribute->getAttributeCode()] = $attribute;
}
}
}
return $this->_usedInProductListing;
Expand All @@ -354,16 +350,13 @@ public function getAttributesUsedInProductListing()
public function getAttributesUsedForSortBy()
{
if (is_null($this->_usedForSortBy)) {
$allAttributes = Mage::getSingleton('eav/config')
->getAttributes(Mage_Catalog_Model_Product::ENTITY);
$this->_usedForSortBy = [];
$entityType = Mage_Catalog_Model_Product::ENTITY;
$attributesData = $this->_getResource()
->getAttributesUsedForSortBy();
Mage::getSingleton('eav/config')
->importAttributesData($entityType, $attributesData);
foreach ($attributesData as $attributeData) {
$attributeCode = $attributeData['attribute_code'];
$this->_usedForSortBy[$attributeCode] = Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode);
foreach ($allAttributes as $attribute) {
if ($attribute->getData('used_for_sort_by')) {
$this->_usedForSortBy[$attribute->getAttributeCode()] = $attribute;
}
}
}
return $this->_usedForSortBy;
Expand Down
31 changes: 20 additions & 11 deletions app/code/core/Mage/Catalog/Model/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,34 @@ public function getCurrentStore()
/**
* Get collection of all filterable attributes for layer products set
*
* @return Mage_Catalog_Model_Resource_Product_Attribute_Collection|array
* @return array
fballiano marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getFilterableAttributes()
{
$setIds = $this->_getSetIds();
if (!$setIds) {
return [];
}
/** @var Mage_Catalog_Model_Resource_Product_Attribute_Collection $collection */
$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection
->setItemObjectClass('catalog/resource_eav_attribute')
->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId())
->setOrder('position', 'ASC');
$collection = $this->_prepareAttributeCollection($collection);
$collection->load();

return $collection;
$eavConfig = Mage::getSingleton('eav/config');
/** @var Mage_Catalog_Model_Resource_Eav_Attribute[] $attributes */
$attributes = [];
foreach ($setIds as $setId) {
$setAttributeIds = $eavConfig->getAttributeSetAttributeIds($setId);
foreach ($setAttributeIds as $attributeId) {
if (!isset($attributes[$attributeId])) {
$attribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeId);
if ($attribute->getIsFilterable()) {
$attributes[$attributeId] = $attribute;
}
}
}
}
usort($attributes, function ($a, $b) {
return $a->getPosition() - $b->getPosition();
});

return $attributes;
}

/**
Expand Down
14 changes: 3 additions & 11 deletions app/code/core/Mage/Catalog/Model/Resource/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,9 @@ public function getChildrenAmount($category, $isActiveFlag = true)
protected function _getIsActiveAttributeId()
{
if ($this->_isActiveAttributeId === null) {
$bind = [
'catalog_category' => Mage_Catalog_Model_Category::ENTITY,
'is_active' => 'is_active',
];
$select = $this->_getReadAdapter()->select()
->from(['a' => $this->getTable('eav/attribute')], ['attribute_id'])
->join(['t' => $this->getTable('eav/entity_type')], 'a.entity_type_id = t.entity_type_id')
->where('entity_type_code = :catalog_category')
->where('attribute_code = :is_active');

$this->_isActiveAttributeId = $this->_getReadAdapter()->fetchOne($select, $bind);
$this->_isActiveAttributeId = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_active')
->getId();
}

return $this->_isActiveAttributeId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ public function getAttribute($attributeCode)
{
$attributes = $this->getAttributes();
if (!isset($attributes[$attributeCode])) {
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode($this->getEntityTypeId(), $attributeCode);
$attribute = Mage::getSingleton('eav/config')
->getAttribute($this->getEntityTypeId(), $attributeCode);
if (!$attribute->getId()) {
Mage::throwException(Mage::helper('catalog')->__('Invalid attribute %s', $attributeCode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getTierPriceAttribute()
{
$data = $this->getData('tier_price_attribute');
if (is_null($data)) {
$data = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'tier_price');
$data = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'tier_price');
$this->setData('tier_price_attribute', $data);
}
return $data;
Expand All @@ -79,7 +79,7 @@ public function getPriceAttribute()
{
$data = $this->getData('price_attribute');
if (is_null($data)) {
$data = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'price');
$data = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'price');
$this->setData('price_attribute', $data);
}
return $data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function getPurchasedSeparatelyAttribute()
if (is_null($this->_purchasedSeparatelyAttribute)) {
$_attributeCode = 'links_purchased_separately';

$this->_purchasedSeparatelyAttribute = Mage::getModel('eav/entity_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, $_attributeCode);
$this->_purchasedSeparatelyAttribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $_attributeCode);
}

return $this->_purchasedSeparatelyAttribute;
Expand Down
Loading