Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
92 changes: 58 additions & 34 deletions app/code/core/Mage/Catalog/Model/Resource/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,19 +580,20 @@ public function getAttributeRawValue($entityId, $attribute, $store)
}

$returnArray = false;
$entityId = (int)$entityId;

if (!is_array($attribute)) {
$attribute = [$attribute];
} elseif (count($attribute) > 1) {
$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 */
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
60 changes: 43 additions & 17 deletions app/code/core/Mage/Catalog/Model/Resource/Collection/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
20 changes: 10 additions & 10 deletions app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,50 +47,50 @@ 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
* If true - allows to prepare final select with analytic functions
*
* @var bool
*/
protected $_useAnalyticFunction = false;
protected $_useAnalyticFunction = false;

/**
* Cast map for attribute order
Expand Down Expand Up @@ -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
*
Expand Down