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 all 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://github.com/davidhiendl",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
4 changes: 3 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 @@ -207,6 +207,7 @@ Do not use 20.x.x if you need IE support.
- removed support for `global/sales/old_fields_map` defined in XML ([#921](https://github.com/OpenMage/magento-lts/pull/921))
- enabled website level config cache ([#2355](https://github.com/OpenMage/magento-lts/pull/2355))
- make overrides of Mage_Core_Model_Resource_Db_Abstract::delete respect parent api ([#1257](https://github.com/OpenMage/magento-lts/pull/1257))
- rewrite Mage_Eav_Model_Config as cache for all eav entity and attribute reads ([#2993](https://github.com/OpenMage/magento-lts/pull/2993))

For full list of changes, you can [compare tags](https://github.com/OpenMage/magento-lts/compare/1.9.4.x...20.0).

Expand Down Expand Up @@ -550,6 +551,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
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/Block/Product/Compare/List.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getItems()
/**
* Retrieve Product Compare Attributes
*
* @return array
* @return Mage_Eav_Model_Entity_Attribute_Abstract[]
*/
public function getAttributes()
{
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(Mage_Catalog_Model_Product::ENTITY);
$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 Mage_Catalog_Model_Resource_Eav_Attribute[]
*/
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 instanceof Mage_Catalog_Model_Resource_Eav_Attribute && $attribute->getIsFilterable()) {
$attributes[$attributeId] = $attribute;
}
}
}
}
usort($attributes, function ($a, $b) {
return $a->getPosition() - $b->getPosition();
});

return $attributes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
* Check whether specified attribute can be used in LN
*
* @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
* @return Mage_Catalog_Model_Resource_Eav_Attribute
* @return int
*/
protected function _getIsFilterableAttribute($attribute)
{
Expand Down
18 changes: 7 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,13 @@ 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);
$attributeId = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_active')
->getId();
if (!is_int($attributeId)) {
Mage::throwException("Failed to find category attribute is_active");
}
$this->_isActiveAttributeId = $attributeId;
}

return $this->_isActiveAttributeId;
Expand Down
10 changes: 9 additions & 1 deletion app/code/core/Mage/Catalog/Model/Resource/Eav/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* @method $this setIsSearchable(int $value)
* @method int getSearchWeight()
* @method $this setSearchWeight(int $value)
* @method int getIsFilterable()
* @method $this setIsFilterable(int $value)
* @method int getIsComparable()
* @method $this setIsComparable(int $value)
Expand Down Expand Up @@ -288,6 +287,15 @@ public function getFrontendLabel()
return $this->_getData('frontend_label');
}

/**
* Retrieve is_filterable value
* @return int
*/
public function getIsFilterable()
colinmollenhour marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->_getData('is_filterable');
}

/**
* Get Attribute translated label for store
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Mage_Catalog_Model_Resource_Product_Compare_Item_Collection extends Mage_C
/**
* Comparable attributes cache
*
* @var array|null
* @var Mage_Eav_Model_Entity_Attribute_Abstract[]|null
*/
protected $_comparableAttributes;

Expand Down Expand Up @@ -211,44 +211,27 @@ protected function _getAttributeIdsBySetIds(array $setIds)
/**
* Retrieve Merged comparable attributes for compared product items
*
* @return array
* @return Mage_Eav_Model_Entity_Attribute_Abstract[]
*/
public function getComparableAttributes()
{
if (is_null($this->_comparableAttributes)) {
$this->_comparableAttributes = [];
$setIds = $this->_getAttributeSetIds();
if ($setIds) {
$select = $this->getConnection()->select()
->from(['main_table' => $this->getTable('eav/attribute')])
->join(
['additional_table' => $this->getTable('catalog/eav_attribute')],
'additional_table.attribute_id=main_table.attribute_id'
)
->joinLeft(
['al' => $this->getTable('eav/attribute_label')],
'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int) $this->getStoreId(),
['store_label' => new Zend_Db_Expr('IFNULL(al.value, main_table.frontend_label)')]
)
->joinLeft(
['ai' => $this->getTable('eav/entity_attribute')],
'ai.attribute_id = main_table.attribute_id'
)
->where('additional_table.is_comparable=?', 1)
->where('ai.attribute_set_id IN(?)', $setIds)
->order(['ai.attribute_group_id ASC', 'ai.sort_order ASC']);
$attributesData = $this->getConnection()->fetchAll($select);
if ($attributesData) {
$entityType = Mage_Catalog_Model_Product::ENTITY;
Mage::getSingleton('eav/config')
->importAttributesData($entityType, $attributesData);
foreach ($attributesData as $data) {
$attribute = Mage::getSingleton('eav/config')
->getAttribute($entityType, $data['attribute_code']);
$eavConfig = Mage::getSingleton('eav/config');
$attributeIds = $eavConfig->getAttributeSetAttributeIds($setIds);
$this->_comparableAttributes = [];
foreach ($attributeIds as $attributeId) {
$attribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeId);
if ($attribute->getData('is_comparable')) {
$this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
}
unset($attributesData);
}

usort($this->_comparableAttributes, function ($a, $b) {
return $a->getPosition() - $b->getPosition();
});
}
}
return $this->_comparableAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ public function getAttributeCodes()

$select->where(implode(' OR ', $whereCondition));
$attributesData = $adapter->fetchAll($select, $bind);
Mage::getSingleton('eav/config')
->importAttributesData($this->getEntityType(), $attributesData);

foreach ($attributesData as $data) {
$this->_attributeCodes[$data['attribute_id']] = $data['attribute_code'];
Expand Down Expand Up @@ -264,8 +262,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 All @@ -274,6 +272,10 @@ public function getAttribute($attributeCode)
->getEntity();
$attribute->setEntity($entity);

if (!($attribute instanceof Mage_Eav_Model_Entity_Attribute)) {
throw new Exception('Product attribute(code=' . $attributeCode . ') is expected to be of type Mage_Eav_Model_Entity_Attribute');
}

return $attribute;
}

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 @@ -33,7 +33,7 @@ class Mage_CatalogSearch_Model_Layer_Filter_Attribute extends Mage_Catalog_Model
* Check whether specified attribute can be used in LN
*
* @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
* @return bool
* @return int
*/
protected function _getIsFilterableAttribute($attribute)
{
Expand Down
Loading