1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Eav_Model_Resource_Entity_Attribute_Option extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: $this->_init('eav/attribute_option', 'option_id');
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: public function addOptionValueToCollection($collection, $attribute, $valueExpr)
54: {
55: $adapter = $this->_getReadAdapter();
56: $attributeCode = $attribute->getAttributeCode();
57: $optionTable1 = $attributeCode . '_option_value_t1';
58: $optionTable2 = $attributeCode . '_option_value_t2';
59: $tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr} AND {$optionTable1}.store_id=0"
60: ;
61: $tableJoinCond2 = $adapter->quoteInto("{$optionTable2}.option_id={$valueExpr} AND {$optionTable2}.store_id=?",
62: $collection->getStoreId());
63: $valueExpr = $adapter->getCheckSql("{$optionTable2}.value_id IS NULL",
64: "{$optionTable1}.value",
65: "{$optionTable2}.value");
66:
67: $collection->getSelect()
68: ->joinLeft(
69: array($optionTable1 => $this->getTable('eav/attribute_option_value')),
70: $tableJoinCond1,
71: array())
72: ->joinLeft(
73: array($optionTable2 => $this->getTable('eav/attribute_option_value')),
74: $tableJoinCond2,
75: array($attributeCode => $valueExpr)
76: );
77:
78: return $this;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88:
89: public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store,
90: $hasValueField = true
91: ) {
92: $adapter = $this->_getReadAdapter();
93: $attributeTable = $attribute->getBackend()->getTable();
94: $attributeCode = $attribute->getAttributeCode();
95:
96: $joinConditionTemplate = "%s.entity_id = %s.entity_id"
97: . " AND %s.entity_type_id = " . $attribute->getEntityTypeId()
98: . " AND %s.attribute_id = " . $attribute->getId()
99: . " AND %s.store_id = %d";
100: $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1',
101: Mage_Core_Model_App::ADMIN_STORE_ID);
102: if ($attribute->getFlatAddChildData()) {
103: $joinCondition .= ' AND e.child_id = t1.entity_id';
104: }
105:
106: $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
107:
108: $select = $adapter->select()
109: ->joinLeft(array('t1' => $attributeTable), $joinCondition, array())
110: ->joinLeft(array('t2' => $attributeTable),
111: sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $store),
112: array($attributeCode => $valueExpr));
113:
114: if (($attribute->getFrontend()->getInputType() != 'multiselect') && $hasValueField) {
115: $valueIdExpr = $adapter->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value');
116: $select
117: ->joinLeft(array('to1' => $this->getTable('eav/attribute_option_value')),
118: "to1.option_id = {$valueExpr} AND to1.store_id = 0", array())
119: ->joinLeft(array('to2' => $this->getTable('eav/attribute_option_value')),
120: $adapter->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store),
121: array($attributeCode . '_value' => $valueIdExpr));
122: }
123:
124: if ($attribute->getFlatAddChildData()) {
125: $select->where('e.is_child = 0');
126: }
127:
128: return $select;
129: }
130: }
131: