diff --git a/app/code/core/Mage/Catalog/Model/Resource/Abstract.php b/app/code/core/Mage/Catalog/Model/Resource/Abstract.php index 504ad543b1e..80393e14930 100644 --- a/app/code/core/Mage/Catalog/Model/Resource/Abstract.php +++ b/app/code/core/Mage/Catalog/Model/Resource/Abstract.php @@ -580,6 +580,7 @@ public function getAttributeRawValue($entityId, $attribute, $store) } $returnArray = false; + $entityId = (int)$entityId; if (!is_array($attribute)) { $attribute = [$attribute]; @@ -587,12 +588,12 @@ public function getAttributeRawValue($entityId, $attribute, $store) $returnArray = true; } - $attributesData = []; - $staticAttributes = []; - $typedAttributes = []; - $staticTable = null; - $adapter = $this->_getReadAdapter(); - $getPerStore = false; + $attributesData = []; + $staticAttributes = []; + $typedAttributes = []; + $staticTable = null; + $adapter = $this->_getReadAdapter(); + $getPerStore = false; foreach ($attribute as $_attribute) { /** @var Mage_Catalog_Model_Entity_Attribute $attribute */ @@ -638,46 +639,69 @@ public function getAttributeRawValue($entityId, $attribute, $store) $store = (int)$store; - foreach ($typedAttributes as $table => $_attributes) { - $select = $adapter->select() - ->from(['default_value' => $table], ['attribute_id']) - ->where('default_value.attribute_id IN (?)', array_keys($_attributes)) - ->where('default_value.entity_type_id = :entity_type_id') - ->where('default_value.entity_id = :entity_id') - ->where('default_value.store_id = ?', 0); - $bind = [ - 'entity_type_id' => $this->getTypeId(), - 'entity_id' => $entityId, - ]; - + foreach ($typedAttributes as $table => $attributes) { + // see also Mage_Catalog_Model_Resource_Collection_Abstract::_getLoadAttributesSelect() if ($getPerStore && $store != $this->getDefaultStoreId()) { + $select = $adapter->select() + ->from(['e' => $this->getEntityTable()], []) + ->where('e.entity_id = ?', $entityId); + // attr join + $select->joinInner( + ['attr' => $table], + implode(' AND ', [ + 'attr.entity_id = e.entity_id', + $adapter->quoteInto('attr.attribute_id IN (?)', array_keys($attributes)), + 'attr.store_id IN (' . $this->getDefaultStoreId() . ', ' . $store . ')', + 'attr.entity_type_id = ' . $this->getTypeId(), + ]), + [] + ); + // default_value join + $select->joinLeft( + ['default_value' => $table], + implode(' AND ', [ + 'default_value.entity_id = e.entity_id', + 'default_value.attribute_id = attr.attribute_id', + 'default_value.store_id = ' . $this->getDefaultStoreId(), + ]), + [] + ); + // store_value join $valueExpr = $adapter->getCheckSql( - 'store_value.value IS NULL', + 'store_value.attribute_id IS NULL', 'default_value.value', 'store_value.value' ); - $joinCondition = [ - 'store_value.attribute_id = default_value.attribute_id', - 'store_value.entity_type_id = :entity_type_id', - 'store_value.entity_id = :entity_id', - 'store_value.store_id = :store_id', - ]; - + $attributeIdExpr = $adapter->getCheckSql( + 'store_value.attribute_id IS NULL', + 'default_value.attribute_id', + 'store_value.attribute_id' + ); $select->joinLeft( ['store_value' => $table], - implode(' AND ', $joinCondition), - ['attr_value' => $valueExpr] + implode(' AND ', [ + 'store_value.entity_id = e.entity_id', + 'store_value.attribute_id = attr.attribute_id', + 'store_value.store_id = ' . $store, + ]), + ['attribute_id' => $attributeIdExpr, 'attr_value' => $valueExpr] ); - - $bind['store_id'] = $store; + $select->group('attr.attribute_id'); } else { - $select->columns(['attr_value' => 'value'], 'default_value'); + $select = $adapter->select() + ->from(['default_value' => $table], ['attribute_id', 'attr_value' => 'value']) + ->where('default_value.entity_id = ?', $entityId) + ->where('default_value.attribute_id IN (?)', array_keys($attributes)) + ->where('default_value.store_id = ?', $this->getDefaultStoreId()) + ->where('default_value.entity_type_id = ?', $this->getTypeId()); } - $result = $adapter->fetchPairs($select, $bind); + $result = $adapter->fetchPairs($select); foreach ($result as $attrId => $value) { - $attrCode = $typedAttributes[$table][$attrId]; - $attributesData[$attrCode] = $value; + if (!empty($attrId)) { + $attrCode = $typedAttributes[$table][$attrId]; + $attributesData[$attrCode] = $value; + } } } } diff --git a/app/code/core/Mage/Catalog/Model/Resource/Collection/Abstract.php b/app/code/core/Mage/Catalog/Model/Resource/Collection/Abstract.php index 2656a117d98..74d960ab7ac 100644 --- a/app/code/core/Mage/Catalog/Model/Resource/Collection/Abstract.php +++ b/app/code/core/Mage/Catalog/Model/Resource/Collection/Abstract.php @@ -95,24 +95,50 @@ protected function _getLoadAttributesSelect($table, $attributeIds = []) $storeId = $this->getStoreId(); if ($storeId) { - $adapter = $this->getConnection(); - $entityIdField = $this->getEntity()->getEntityIdField(); - $joinCondition = [ - 't_s.attribute_id = t_d.attribute_id', - 't_s.entity_id = t_d.entity_id', - $adapter->quoteInto('t_s.store_id = ?', $storeId) - ]; + $adapter = $this->getConnection(); + $entity = $this->getEntity(); + + // see also Mage_Catalog_Model_Resource_Abstract::getAttributeRawValue() $select = $adapter->select() - ->from(['t_d' => $table], [$entityIdField, 'attribute_id']) - ->joinLeft( - ['t_s' => $table], - implode(' AND ', $joinCondition), - [] - ) - ->where('t_d.entity_type_id = ?', $this->getEntity()->getTypeId()) - ->where("t_d.{$entityIdField} IN (?)", array_keys($this->_itemsById)) - ->where('t_d.attribute_id IN (?)', $attributeIds) - ->where('t_d.store_id = ?', 0); + ->from(['e' => $entity->getEntityTable()], []) + ->where('e.entity_id IN (?)', array_keys($this->_itemsById)); + // attr join + $select->joinInner( + ['attr' => $table], + implode(' AND ', [ + 'attr.entity_id = e.entity_id', + $adapter->quoteInto('attr.attribute_id IN (?)', $attributeIds), + 'attr.store_id IN (' . $this->getDefaultStoreId() . ', ' . $storeId . ')', + 'attr.entity_type_id = ' . $entity->getTypeId(), + ]), + [] + ); + // t_d join + $select->joinLeft( + ['t_d' => $table], + implode(' AND ', [ + 't_d.entity_id = e.entity_id', + 't_d.attribute_id = attr.attribute_id', + 't_d.store_id = ' . $this->getDefaultStoreId(), + ]), + [] + ); + // t_s join + $attributeIdExpr = $adapter->getCheckSql( + 't_s.attribute_id IS NULL', + 't_d.attribute_id', + 't_s.attribute_id' + ); + $select->joinLeft( + ['t_s' => $table], + implode(' AND ', [ + 't_s.entity_id = e.entity_id', + 't_s.attribute_id = attr.attribute_id', + 't_s.store_id = ' . $storeId, + ]), + ['e.entity_id', 'attribute_id' => $attributeIdExpr] + ); + $select->group('e.entity_id')->group('attr.attribute_id'); } else { $select = parent::_getLoadAttributesSelect($table) ->where('store_id = ?', $this->getDefaultStoreId()); diff --git a/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php b/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php index e1554f5f10f..5c19d8951b3 100644 --- a/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php +++ b/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php @@ -26,14 +26,14 @@ abstract class Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Col * * @var array */ - protected $_itemsById = []; + protected $_itemsById = []; /** * Entity static fields * * @var array */ - protected $_staticFields = []; + protected $_staticFields = []; /** * Entity object to define collection's attributes @@ -47,42 +47,42 @@ abstract class Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Col * * @var array */ - protected $_selectEntityTypes = []; + protected $_selectEntityTypes = []; /** * Attributes to be fetched for objects in collection * * @var array */ - protected $_selectAttributes = []; + protected $_selectAttributes = []; /** * Attributes to be filtered order sorted by * * @var array */ - protected $_filterAttributes = []; + protected $_filterAttributes = []; /** * Joined entities * * @var array */ - protected $_joinEntities = []; + protected $_joinEntities = []; /** * Joined attributes * * @var array */ - protected $_joinAttributes = []; + protected $_joinAttributes = []; /** * Joined fields data * * @var array */ - protected $_joinFields = []; + protected $_joinFields = []; /** * Use analytic function flag @@ -90,7 +90,7 @@ abstract class Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Col * * @var bool */ - protected $_useAnalyticFunction = false; + protected $_useAnalyticFunction = false; /** * Cast map for attribute order @@ -1181,7 +1181,7 @@ protected function _addLoadAttributesSelectValues($select, $table, $type) } /** - * Initialize entity ubject property value + * Initialize entity object property value * * $valueInfo is _getLoadAttributesSelect fetch result row *