PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Ced/CsMarketplace/Model/Mysql4/Setup/Abstract.php

https://gitlab.com/vincent.perdereau/picandparts
PHP | 284 lines | 222 code | 21 blank | 41 comment | 12 complexity | 2a46362f912997cff723135137722ecd MD5 | raw file
  1. <?php
  2. /**
  3. * CedCommerce
  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. *
  12. * @category Ced
  13. * @package Ced_CsMarketplace
  14. * @author CedCommerce Core Team <coreteam@cedcommerce.com>
  15. * @copyright Copyright CedCommerce (http://cedcommerce.com/)
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. */
  18. /**
  19. * CsMarketplace abstract resource model
  20. *
  21. * @category Ced
  22. * @package Ced_CsMarketplace
  23. * @author CedCommerce Core Team <coreteam@cedcommerce.com>
  24. */
  25. class Ced_CsMarketplace_Model_Mysql4_Setup_Abstract extends Mage_Eav_Model_Entity_Setup
  26. {
  27. /**
  28. * Create entity tables
  29. *
  30. * @param string $baseName
  31. * @param array $options
  32. * - no-main
  33. * - no-default-types
  34. * - types
  35. * @return unknown
  36. */
  37. public function createEntityTables($baseTableName, array $options = array())
  38. {
  39. if(version_compare(Mage::getVersion(), '1.6', '<')) {
  40. return $this->createEntityTablesBelow16($baseTableName, $options);
  41. } else {
  42. return $this->createEntityTablesAbove16($baseTableName, $options);
  43. }
  44. }
  45. public function createEntityTablesBelow16($baseName, array $options=array())
  46. {
  47. $sql = '';
  48. if (empty($options['no-main'])) {
  49. $sql = "
  50. DROP TABLE IF EXISTS `{$baseName}`;
  51. CREATE TABLE `{$baseName}` (
  52. `entity_id` int(10) unsigned NOT NULL auto_increment,
  53. `entity_type_id` smallint(8) unsigned NOT NULL default '0',
  54. `attribute_set_id` smallint(5) unsigned NOT NULL default '0',
  55. `increment_id` varchar(50) NOT NULL default '',
  56. `parent_id` int(10) unsigned NULL default '0',
  57. `store_id` smallint(5) unsigned NOT NULL default '0',
  58. `created_at` datetime NOT NULL default '0000-00-00 00:00:00',
  59. `updated_at` datetime NOT NULL default '0000-00-00 00:00:00',
  60. `is_active` tinyint(1) unsigned NOT NULL default '1',
  61. PRIMARY KEY (`entity_id`),
  62. CONSTRAINT `FK_{$baseName}_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav/entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  63. CONSTRAINT `FK_{$baseName}_store` FOREIGN KEY (`store_id`) REFERENCES `{$this->getTable('core/store')}` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
  64. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  65. }
  66. $types = array(
  67. 'datetime'=>'datetime',
  68. 'decimal'=>'decimal(12,4)',
  69. 'int'=>'int',
  70. 'text'=>'text',
  71. 'varchar'=>'varchar(255)',
  72. );
  73. if (!empty($options['types']) && is_array($options['types'])) {
  74. if ($options['no-default-types']) {
  75. $types = array();
  76. }
  77. $types = array_merge($types, $options['types']);
  78. }
  79. foreach ($types as $type=>$fieldType) {
  80. $sql .= "
  81. DROP TABLE IF EXISTS `{$baseName}_{$type}`;
  82. CREATE TABLE `{$baseName}_{$type}` (
  83. `value_id` int(11) NOT NULL auto_increment,
  84. `entity_type_id` smallint(8) unsigned NOT NULL default '0',
  85. `attribute_id` smallint(5) unsigned NOT NULL default '0',
  86. `store_id` smallint(5) unsigned NOT NULL default '0',
  87. `entity_id` int(10) unsigned NOT NULL default '0',
  88. `value` {$fieldType} NOT NULL,
  89. PRIMARY KEY (`value_id`),
  90. UNIQUE KEY `IDX_BASE` (`entity_type_id`,`entity_id`,`attribute_id`,`store_id`),
  91. ".($type!=='text' ? "
  92. KEY `value_by_attribute` (`attribute_id`,`value`),
  93. KEY `value_by_entity_type` (`entity_type_id`,`value`),
  94. " : "")."
  95. CONSTRAINT `FK_{$baseName}_{$type}` FOREIGN KEY (`entity_id`) REFERENCES `{$baseName}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  96. CONSTRAINT `FK_{$baseName}_{$type}_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `{$this->getTable('eav/attribute')}` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  97. CONSTRAINT `FK_{$baseName}_{$type}_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `{$this->getTable('eav/entity_type')}` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  98. CONSTRAINT `FK_{$baseName}_{$type}_store` FOREIGN KEY (`store_id`) REFERENCES `{$this->getTable('core/store')}` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
  99. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  100. }
  101. try {
  102. $this->_conn->multi_query($sql);
  103. } catch (Exception $e) {
  104. throw $e;
  105. }
  106. return $this;
  107. }
  108. public function createEntityTablesAbove16($baseTableName, array $options = array())
  109. {
  110. $isNoCreateMainTable = $this->_getValue($options, 'no-main', false);
  111. $isNoDefaultTypes = $this->_getValue($options, 'no-default-types', false);
  112. $customTypes = $this->_getValue($options, 'types', array());
  113. $tables = array();
  114. if (!$isNoCreateMainTable) {
  115. /**
  116. * Create table main eav table
  117. */
  118. $connection = $this->getConnection();
  119. $mainTable = $connection
  120. ->newTable($this->getTable($baseTableName))
  121. ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
  122. 'identity' => true,
  123. 'nullable' => false,
  124. 'primary' => true,
  125. 'unsigned' => true,
  126. ), 'Entity Id')
  127. ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  128. 'unsigned' => true,
  129. 'nullable' => false,
  130. 'default' => '0',
  131. ), 'Entity Type Id')
  132. ->addColumn('attribute_set_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  133. 'unsigned' => true,
  134. 'nullable' => false,
  135. 'default' => '0',
  136. ), 'Attribute Set Id')
  137. ->addColumn('increment_id', Varien_Db_Ddl_Table::TYPE_TEXT, 50, array(
  138. 'nullable' => false,
  139. 'default' => '',
  140. ), 'Increment Id')
  141. ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  142. 'unsigned' => true,
  143. 'nullable' => false,
  144. 'default' => '0',
  145. ), 'Store Id')
  146. ->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
  147. 'nullable' => false,
  148. ), 'Created At')
  149. ->addColumn('updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
  150. 'nullable' => false,
  151. ), 'Updated At')
  152. ->addColumn('is_active', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  153. 'unsigned' => true,
  154. 'nullable' => false,
  155. 'default' => '1',
  156. ), 'Defines Is Entity Active')
  157. ->addIndex($this->getIdxName($baseTableName, array('entity_type_id')),
  158. array('entity_type_id'))
  159. ->addIndex($this->getIdxName($baseTableName, array('store_id')),
  160. array('store_id'))
  161. ->addForeignKey($this->getFkName($baseTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
  162. 'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
  163. Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
  164. ->addForeignKey($this->getFkName($baseTableName, 'store_id', 'core/store', 'store_id'),
  165. 'store_id', $this->getTable('core/store'), 'store_id',
  166. Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
  167. ->setComment('Eav Entity Main Table');
  168. $tables[$this->getTable($baseTableName)] = $mainTable;
  169. }
  170. $types = array();
  171. if (!$isNoDefaultTypes) {
  172. $types = array(
  173. 'datetime' => array(Varien_Db_Ddl_Table::TYPE_DATETIME, null),
  174. 'decimal' => array(Varien_Db_Ddl_Table::TYPE_DECIMAL, '12,4'),
  175. 'int' => array(Varien_Db_Ddl_Table::TYPE_INTEGER, null),
  176. 'text' => array(Varien_Db_Ddl_Table::TYPE_TEXT, '64k'),
  177. 'varchar' => array(Varien_Db_Ddl_Table::TYPE_TEXT, '255'),
  178. 'char' => array(Varien_Db_Ddl_Table::TYPE_TEXT, '255')
  179. );
  180. }
  181. if (!empty($customTypes)) {
  182. foreach ($customTypes as $type => $fieldType) {
  183. if (count($fieldType) != 2) {
  184. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong type definition for %s', $type));
  185. }
  186. $types[$type] = $fieldType;
  187. }
  188. }
  189. /**
  190. * Create table array($baseTableName, $type)
  191. */
  192. foreach ($types as $type => $fieldType) {
  193. $eavTableName = array($baseTableName, $type);
  194. $eavTable = $connection->newTable($this->getTable($eavTableName));
  195. $eavTable
  196. ->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
  197. 'identity' => true,
  198. 'nullable' => false,
  199. 'primary' => true,
  200. 'unsigned' => true,
  201. ), 'Value Id')
  202. ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  203. 'unsigned' => true,
  204. 'nullable' => false,
  205. 'default' => '0',
  206. ), 'Entity Type Id')
  207. ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  208. 'unsigned' => true,
  209. 'nullable' => false,
  210. 'default' => '0',
  211. ), 'Attribute Id')
  212. ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
  213. 'unsigned' => true,
  214. 'nullable' => false,
  215. 'default' => '0',
  216. ), 'Store Id')
  217. ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
  218. 'unsigned' => true,
  219. 'nullable' => false,
  220. 'default' => '0',
  221. ), 'Entity Id')
  222. ->addColumn('value', $fieldType[0], $fieldType[1], array(
  223. 'nullable' => false,
  224. ), 'Attribute Value')
  225. ->addIndex($this->getIdxName($eavTableName, array('entity_type_id')),
  226. array('entity_type_id'))
  227. ->addIndex($this->getIdxName($eavTableName, array('attribute_id')),
  228. array('attribute_id'))
  229. ->addIndex($this->getIdxName($eavTableName, array('store_id')),
  230. array('store_id'))
  231. ->addIndex($this->getIdxName($eavTableName, array('entity_id')),
  232. array('entity_id'));
  233. if ($type !== 'text') {
  234. $eavTable->addIndex($this->getIdxName($eavTableName, array('attribute_id', 'value')),
  235. array('attribute_id', 'value'));
  236. $eavTable->addIndex($this->getIdxName($eavTableName, array('entity_type_id', 'value')),
  237. array('entity_type_id', 'value'));
  238. }
  239. $eavTable
  240. ->addForeignKey($this->getFkName($eavTableName, 'entity_id', $baseTableName, 'entity_id'),
  241. 'entity_id', $this->getTable($baseTableName), 'entity_id',
  242. Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
  243. ->addForeignKey($this->getFkName($eavTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
  244. 'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
  245. Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
  246. ->addForeignKey($this->getFkName($eavTableName, 'store_id', 'core/store', 'store_id'),
  247. 'store_id', $this->getTable('core/store'), 'store_id',
  248. Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
  249. ->setComment('Eav Entity Value Table');
  250. $tables[$this->getTable($eavTableName)] = $eavTable;
  251. }
  252. // DDL operations are forbidden within transactions
  253. // See Varien_Db_Adapter_Pdo_Mysql::_checkDdlTransaction()
  254. try {
  255. foreach ($tables as $tableName => $table) {
  256. $connection->createTable($table);
  257. }
  258. } catch (Exception $e) {
  259. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Can\'t create table: %s', $tableName));
  260. }
  261. return $this;
  262. }
  263. }