Skip to content

Commit 157bb9a

Browse files
authored
Fixed getAttributeRawValue when there are no value in the "default" store (#2964)
1 parent 9ffc7c1 commit 157bb9a

File tree

3 files changed

+111
-61
lines changed

3 files changed

+111
-61
lines changed

app/code/core/Mage/Catalog/Model/Resource/Abstract.php

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -580,19 +580,20 @@ public function getAttributeRawValue($entityId, $attribute, $store)
580580
}
581581

582582
$returnArray = false;
583+
$entityId = (int)$entityId;
583584

584585
if (!is_array($attribute)) {
585586
$attribute = [$attribute];
586587
} elseif (count($attribute) > 1) {
587588
$returnArray = true;
588589
}
589590

590-
$attributesData = [];
591-
$staticAttributes = [];
592-
$typedAttributes = [];
593-
$staticTable = null;
594-
$adapter = $this->_getReadAdapter();
595-
$getPerStore = false;
591+
$attributesData = [];
592+
$staticAttributes = [];
593+
$typedAttributes = [];
594+
$staticTable = null;
595+
$adapter = $this->_getReadAdapter();
596+
$getPerStore = false;
596597

597598
foreach ($attribute as $_attribute) {
598599
/** @var Mage_Catalog_Model_Entity_Attribute $attribute */
@@ -638,46 +639,69 @@ public function getAttributeRawValue($entityId, $attribute, $store)
638639

639640
$store = (int)$store;
640641

641-
foreach ($typedAttributes as $table => $_attributes) {
642-
$select = $adapter->select()
643-
->from(['default_value' => $table], ['attribute_id'])
644-
->where('default_value.attribute_id IN (?)', array_keys($_attributes))
645-
->where('default_value.entity_type_id = :entity_type_id')
646-
->where('default_value.entity_id = :entity_id')
647-
->where('default_value.store_id = ?', 0);
648-
$bind = [
649-
'entity_type_id' => $this->getTypeId(),
650-
'entity_id' => $entityId,
651-
];
652-
642+
foreach ($typedAttributes as $table => $attributes) {
643+
// see also Mage_Catalog_Model_Resource_Collection_Abstract::_getLoadAttributesSelect()
653644
if ($getPerStore && $store != $this->getDefaultStoreId()) {
645+
$select = $adapter->select()
646+
->from(['e' => $this->getEntityTable()], [])
647+
->where('e.entity_id = ?', $entityId);
648+
// attr join
649+
$select->joinInner(
650+
['attr' => $table],
651+
implode(' AND ', [
652+
'attr.entity_id = e.entity_id',
653+
$adapter->quoteInto('attr.attribute_id IN (?)', array_keys($attributes)),
654+
'attr.store_id IN (' . $this->getDefaultStoreId() . ', ' . $store . ')',
655+
'attr.entity_type_id = ' . $this->getTypeId(),
656+
]),
657+
[]
658+
);
659+
// default_value join
660+
$select->joinLeft(
661+
['default_value' => $table],
662+
implode(' AND ', [
663+
'default_value.entity_id = e.entity_id',
664+
'default_value.attribute_id = attr.attribute_id',
665+
'default_value.store_id = ' . $this->getDefaultStoreId(),
666+
]),
667+
[]
668+
);
669+
// store_value join
654670
$valueExpr = $adapter->getCheckSql(
655-
'store_value.value IS NULL',
671+
'store_value.attribute_id IS NULL',
656672
'default_value.value',
657673
'store_value.value'
658674
);
659-
$joinCondition = [
660-
'store_value.attribute_id = default_value.attribute_id',
661-
'store_value.entity_type_id = :entity_type_id',
662-
'store_value.entity_id = :entity_id',
663-
'store_value.store_id = :store_id',
664-
];
665-
675+
$attributeIdExpr = $adapter->getCheckSql(
676+
'store_value.attribute_id IS NULL',
677+
'default_value.attribute_id',
678+
'store_value.attribute_id'
679+
);
666680
$select->joinLeft(
667681
['store_value' => $table],
668-
implode(' AND ', $joinCondition),
669-
['attr_value' => $valueExpr]
682+
implode(' AND ', [
683+
'store_value.entity_id = e.entity_id',
684+
'store_value.attribute_id = attr.attribute_id',
685+
'store_value.store_id = ' . $store,
686+
]),
687+
['attribute_id' => $attributeIdExpr, 'attr_value' => $valueExpr]
670688
);
671-
672-
$bind['store_id'] = $store;
689+
$select->group('attr.attribute_id');
673690
} else {
674-
$select->columns(['attr_value' => 'value'], 'default_value');
691+
$select = $adapter->select()
692+
->from(['default_value' => $table], ['attribute_id', 'attr_value' => 'value'])
693+
->where('default_value.entity_id = ?', $entityId)
694+
->where('default_value.attribute_id IN (?)', array_keys($attributes))
695+
->where('default_value.store_id = ?', $this->getDefaultStoreId())
696+
->where('default_value.entity_type_id = ?', $this->getTypeId());
675697
}
676698

677-
$result = $adapter->fetchPairs($select, $bind);
699+
$result = $adapter->fetchPairs($select);
678700
foreach ($result as $attrId => $value) {
679-
$attrCode = $typedAttributes[$table][$attrId];
680-
$attributesData[$attrCode] = $value;
701+
if (!empty($attrId)) {
702+
$attrCode = $typedAttributes[$table][$attrId];
703+
$attributesData[$attrCode] = $value;
704+
}
681705
}
682706
}
683707
}

app/code/core/Mage/Catalog/Model/Resource/Collection/Abstract.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,50 @@ protected function _getLoadAttributesSelect($table, $attributeIds = [])
9595
$storeId = $this->getStoreId();
9696

9797
if ($storeId) {
98-
$adapter = $this->getConnection();
99-
$entityIdField = $this->getEntity()->getEntityIdField();
100-
$joinCondition = [
101-
't_s.attribute_id = t_d.attribute_id',
102-
't_s.entity_id = t_d.entity_id',
103-
$adapter->quoteInto('t_s.store_id = ?', $storeId)
104-
];
98+
$adapter = $this->getConnection();
99+
$entity = $this->getEntity();
100+
101+
// see also Mage_Catalog_Model_Resource_Abstract::getAttributeRawValue()
105102
$select = $adapter->select()
106-
->from(['t_d' => $table], [$entityIdField, 'attribute_id'])
107-
->joinLeft(
108-
['t_s' => $table],
109-
implode(' AND ', $joinCondition),
110-
[]
111-
)
112-
->where('t_d.entity_type_id = ?', $this->getEntity()->getTypeId())
113-
->where("t_d.{$entityIdField} IN (?)", array_keys($this->_itemsById))
114-
->where('t_d.attribute_id IN (?)', $attributeIds)
115-
->where('t_d.store_id = ?', 0);
103+
->from(['e' => $entity->getEntityTable()], [])
104+
->where('e.entity_id IN (?)', array_keys($this->_itemsById));
105+
// attr join
106+
$select->joinInner(
107+
['attr' => $table],
108+
implode(' AND ', [
109+
'attr.entity_id = e.entity_id',
110+
$adapter->quoteInto('attr.attribute_id IN (?)', $attributeIds),
111+
'attr.store_id IN (' . $this->getDefaultStoreId() . ', ' . $storeId . ')',
112+
'attr.entity_type_id = ' . $entity->getTypeId(),
113+
]),
114+
[]
115+
);
116+
// t_d join
117+
$select->joinLeft(
118+
['t_d' => $table],
119+
implode(' AND ', [
120+
't_d.entity_id = e.entity_id',
121+
't_d.attribute_id = attr.attribute_id',
122+
't_d.store_id = ' . $this->getDefaultStoreId(),
123+
]),
124+
[]
125+
);
126+
// t_s join
127+
$attributeIdExpr = $adapter->getCheckSql(
128+
't_s.attribute_id IS NULL',
129+
't_d.attribute_id',
130+
't_s.attribute_id'
131+
);
132+
$select->joinLeft(
133+
['t_s' => $table],
134+
implode(' AND ', [
135+
't_s.entity_id = e.entity_id',
136+
't_s.attribute_id = attr.attribute_id',
137+
't_s.store_id = ' . $storeId,
138+
]),
139+
['e.entity_id', 'attribute_id' => $attributeIdExpr]
140+
);
141+
$select->group('e.entity_id')->group('attr.attribute_id');
116142
} else {
117143
$select = parent::_getLoadAttributesSelect($table)
118144
->where('store_id = ?', $this->getDefaultStoreId());

app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ abstract class Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Col
2626
*
2727
* @var array
2828
*/
29-
protected $_itemsById = [];
29+
protected $_itemsById = [];
3030

3131
/**
3232
* Entity static fields
3333
*
3434
* @var array
3535
*/
36-
protected $_staticFields = [];
36+
protected $_staticFields = [];
3737

3838
/**
3939
* Entity object to define collection's attributes
@@ -47,50 +47,50 @@ abstract class Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Col
4747
*
4848
* @var array
4949
*/
50-
protected $_selectEntityTypes = [];
50+
protected $_selectEntityTypes = [];
5151

5252
/**
5353
* Attributes to be fetched for objects in collection
5454
*
5555
* @var array
5656
*/
57-
protected $_selectAttributes = [];
57+
protected $_selectAttributes = [];
5858

5959
/**
6060
* Attributes to be filtered order sorted by
6161
*
6262
* @var array
6363
*/
64-
protected $_filterAttributes = [];
64+
protected $_filterAttributes = [];
6565

6666
/**
6767
* Joined entities
6868
*
6969
* @var array
7070
*/
71-
protected $_joinEntities = [];
71+
protected $_joinEntities = [];
7272

7373
/**
7474
* Joined attributes
7575
*
7676
* @var array
7777
*/
78-
protected $_joinAttributes = [];
78+
protected $_joinAttributes = [];
7979

8080
/**
8181
* Joined fields data
8282
*
8383
* @var array
8484
*/
85-
protected $_joinFields = [];
85+
protected $_joinFields = [];
8686

8787
/**
8888
* Use analytic function flag
8989
* If true - allows to prepare final select with analytic functions
9090
*
9191
* @var bool
9292
*/
93-
protected $_useAnalyticFunction = false;
93+
protected $_useAnalyticFunction = false;
9494

9595
/**
9696
* Cast map for attribute order
@@ -1181,7 +1181,7 @@ protected function _addLoadAttributesSelectValues($select, $table, $type)
11811181
}
11821182

11831183
/**
1184-
* Initialize entity ubject property value
1184+
* Initialize entity object property value
11851185
*
11861186
* $valueInfo is _getLoadAttributesSelect fetch result row
11871187
*

0 commit comments

Comments
 (0)