1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Catalog Product Flat resource model
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Model_Resource_Product_Flat extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Store scope Id
39: *
40: * @var int
41: */
42: protected $_storeId;
43:
44: /**
45: * Init connection and resource table
46: *
47: */
48: protected function _construct()
49: {
50: $this->_init('catalog/product_flat', 'entity_id');
51: $this->_storeId = (int)Mage::app()->getStore()->getId();
52: }
53:
54: /**
55: * Retrieve store for resource model
56: *
57: * @return int
58: */
59: public function getStoreId()
60: {
61: return $this->_storeId;
62: }
63:
64: /**
65: * Set store for resource model
66: *
67: * @param mixed $store
68: * @return Mage_Catalog_Model_Resource_Product_Flat
69: */
70: public function setStoreId($store)
71: {
72: if (is_int($store)) {
73: $this->_storeId = $store;
74: } else {
75: $this->_storeId = (int)Mage::app()->getStore($store)->getId();
76: }
77: return $this;
78: }
79:
80: /**
81: * Retrieve Flat Table name
82: *
83: * @param mixed $store
84: * @return string
85: */
86: public function getFlatTableName($store = null)
87: {
88: if ($store === null) {
89: $store = $this->getStoreId();
90: }
91: return $this->getTable(array('catalog/product_flat', $store));
92: }
93:
94: /**
95: * Retrieve entity type id
96: *
97: * @return int
98: */
99: public function getTypeId()
100: {
101: return Mage::getSingleton('catalog/config')
102: ->getEntityType(Mage_Catalog_Model_Product::ENTITY)
103: ->getEntityTypeId();
104: }
105:
106: /**
107: * Retrieve attribute columns for collection select
108: *
109: * @param string $attributeCode
110: * @return array|null
111: */
112: public function getAttributeForSelect($attributeCode)
113: {
114: $describe = $this->_getWriteAdapter()->describeTable($this->getFlatTableName());
115: if (!isset($describe[$attributeCode])) {
116: return null;
117: }
118: $columns = array($attributeCode => $attributeCode);
119:
120: $attributeIndex = sprintf('%s_value', $attributeCode);
121: if (isset($describe[$attributeIndex])) {
122: $columns[$attributeIndex] = $attributeIndex;
123: }
124:
125: return $columns;
126: }
127:
128: /**
129: * Retrieve Attribute Sort column name
130: *
131: * @param string $attributeCode
132: * @return string
133: */
134: public function getAttributeSortColumn($attributeCode)
135: {
136: $describe = $this->_getWriteAdapter()->describeTable($this->getFlatTableName());
137: if (!isset($describe[$attributeCode])) {
138: return null;
139: }
140: $attributeIndex = sprintf('%s_value', $attributeCode);
141: if (isset($describe[$attributeIndex])) {
142: return $attributeIndex;
143: }
144: return $attributeCode;
145: }
146:
147: /**
148: * Retrieve Flat Table columns list
149: *
150: * @return array
151: */
152: public function getAllTableColumns()
153: {
154: $describe = $this->_getWriteAdapter()->describeTable($this->getFlatTableName());
155: return array_keys($describe);
156: }
157:
158: /**
159: * Check whether the attribute is a real field in entity table
160: * Rewrited for EAV Collection
161: *
162: * @param integer|string|Mage_Eav_Model_Entity_Attribute_Abstract $attribute
163: * @return bool
164: */
165: public function isAttributeStatic($attribute)
166: {
167: $attributeCode = null;
168: if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Interface) {
169: $attributeCode = $attribute->getAttributeCode();
170: } elseif (is_string($attribute)) {
171: $attributeCode = $attribute;
172: } elseif (is_numeric($attribute)) {
173: $attributeCode = $this->getAttribute($attribute)
174: ->getAttributeCode();
175: }
176:
177: if ($attributeCode) {
178: $columns = $this->getAllTableColumns();
179: if (in_array($attributeCode, $columns)) {
180: return true;
181: }
182: }
183:
184: return false;
185: }
186:
187: /**
188: * Retrieve entity id field name in entity table
189: * Rewrited for EAV collection compatible
190: *
191: * @return string
192: */
193: public function getEntityIdField()
194: {
195: return $this->getIdFieldName();
196: }
197:
198: /**
199: * Retrieve attribute instance
200: * Special for non static flat table
201: *
202: * @param mixed $attribute
203: * @return Mage_Eav_Model_Entity_Attribute_Abstract
204: */
205: public function getAttribute($attribute)
206: {
207: return Mage::getSingleton('catalog/config')
208: ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute);
209: }
210:
211: /**
212: * Retrieve main resource table name
213: *
214: * @return string
215: */
216: public function getMainTable()
217: {
218: return $this->getFlatTableName($this->getStoreId());
219: }
220: }
221: