00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract extends Mage_Eav_Model_Entity_Collection_Abstract
00037 {
00038 protected $_storeId = null;
00039
00040 public function setStore($store)
00041 {
00042 $this->setStoreId(Mage::app()->getStore($store)->getId());
00043 return $this;
00044 }
00045
00046 public function setStoreId($storeId)
00047 {
00048 if ($storeId instanceof Mage_Core_Model_Store) {
00049 $storeId = $storeId->getId();
00050 }
00051 $this->_storeId = $storeId;
00052 return $this;
00053 }
00054
00055 public function getStoreId()
00056 {
00057 if (is_null($this->_storeId)) {
00058 $this->setStoreId(Mage::app()->getStore()->getId());
00059 }
00060 return $this->_storeId;
00061 }
00062
00063 public function getDefaultStoreId()
00064 {
00065 return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 protected function _getLoadAttributesSelect($table, $attributeIds = array())
00075 {
00076 if (empty($attributeIds)) {
00077 $attributeIds = $this->_selectAttributes;
00078 }
00079 if ((int) $this->getStoreId()) {
00080 $entityIdField = $this->getEntity()->getEntityIdField();
00081 $joinCondition = 'store.attribute_id=default.attribute_id
00082 AND store.entity_id=default.entity_id
00083 AND store.store_id='.(int) $this->getStoreId();
00084
00085 $select = $this->getConnection()->select()
00086 ->from(array('default'=>$table), array($entityIdField, 'attribute_id', 'default_value'=>'value'))
00087 ->joinLeft(
00088 array('store'=>$table),
00089 $joinCondition,
00090 array(
00091 'store_value' => 'value',
00092 'value' => new Zend_Db_Expr('IFNULL(store.value, default.value)')
00093 )
00094 )
00095 ->where('default.entity_type_id=?', $this->getEntity()->getTypeId())
00096 ->where("default.$entityIdField in (?)", array_keys($this->_itemsById))
00097 ->where('default.attribute_id in (?)', $attributeIds)
00098 ->where('default.store_id = 0');
00099 }
00100 else {
00101 $select = parent::_getLoadAttributesSelect($table)
00102 ->where('store_id=?', $this->getDefaultStoreId());
00103 }
00104 return $select;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 protected function _joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
00142 {
00143 if (isset($this->_joinAttributes[$fieldCode]['store_id'])) {
00144 $store_id = $this->_joinAttributes[$fieldCode]['store_id'];
00145 }
00146 else {
00147 $store_id = $this->getStoreId();
00148 }
00149
00150 if ($store_id != $this->getDefaultStoreId() && !$attribute->isScopeGlobal()) {
00151
00152
00153
00154
00155 $defCondition = '('.join(') AND (', $condition).')';
00156 $defAlias = $tableAlias.'_default';
00157 $defFieldCode = $fieldCode.'_default';
00158 $defFieldAlias= str_replace($tableAlias, $defAlias, $fieldAlias);
00159
00160 $defCondition = str_replace($tableAlias, $defAlias, $defCondition);
00161 $defCondition.= $this->getConnection()->quoteInto(" AND $defAlias.store_id=?", $this->getDefaultStoreId());
00162
00163 $this->getSelect()->$method(
00164 array($defAlias => $attribute->getBackend()->getTable()),
00165 $defCondition,
00166 array()
00167 );
00168
00169 $method = 'joinLeft';
00170 $fieldAlias = new Zend_Db_Expr("IFNULL($fieldAlias, $defFieldAlias)");
00171 $this->_joinAttributes[$fieldCode]['condition_alias'] = $fieldAlias;
00172 $this->_joinAttributes[$fieldCode]['attribute'] = $attribute;
00173 }
00174 else {
00175 $store_id = $this->getDefaultStoreId();
00176 }
00177 $condition[] = $this->getConnection()->quoteInto("$tableAlias.store_id=?", $store_id);
00178 return parent::_joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias);
00179 }
00180 }