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 $installer = $this;
00028
00029
00030 $installer->startSetup();
00031 $installer->run("
00032 DROP TABLE IF EXISTS {$this->getTable('catalog_category_product_index')};
00033 CREATE TABLE `{$installer->getTable('catalog_category_product_index')}` (
00034 `category_id` int(10) unsigned NOT NULL default '0',
00035 `product_id` int(10) unsigned NOT NULL default '0',
00036 `position` int(10) unsigned NOT NULL default '0',
00037 `is_parent` tinyint(1) unsigned NOT NULL default '0',
00038 UNIQUE KEY `UNQ_CATEGORY_PRODUCT` (`category_id`,`product_id`),
00039 KEY `IDX_CATEGORY_POSITION` (`category_id`,`position`),
00040 CONSTRAINT `FK_CATALOG_CATEGORY_PRODUCT_INDEX_PRODUCT_ENTITY` FOREIGN KEY (`product_id`) REFERENCES `{$installer->getTable('catalog_product_entity')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00041 CONSTRAINT `FK_CATALOG_CATEGORY_PRODUCT_INDEX_CATEGORY_ENTITY` FOREIGN KEY (`category_id`) REFERENCES `{$installer->getTable('catalog_category_entity')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE
00042 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00043
00044 DROP TABLE IF EXISTS {$this->getTable('catalog_product_enabled_index')};
00045 CREATE TABLE `{$installer->getTable('catalog_product_enabled_index')}` (
00046 `product_id` int(10) unsigned NOT NULL default '0',
00047 `store_id` smallint(5) unsigned NOT NULL default '0',
00048 `visibility` smallint(5) unsigned NOT NULL default '0',
00049 UNIQUE KEY `UNQ_PRODUCT_STORE` (`product_id`,`store_id`),
00050 KEY `IDX_PRODUCT_VISIBILITY_IN_STORE` (`product_id`,`store_id`, `visibility`),
00051 CONSTRAINT `FK_CATALOG_PRODUCT_ENABLED_INDEX_PRODUCT_ENTITY` FOREIGN KEY (`product_id`) REFERENCES `{$installer->getTable('catalog_product_entity')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00052 CONSTRAINT `FK_CATALOG_PRODUCT_ENABLED_INDEX_STORE` FOREIGN KEY (`store_id`) REFERENCES `{$installer->getTable('core_store')}` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
00053 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
00054 ");
00055
00056
00057
00058
00059 $categories = $installer->getConnection()->fetchAll(
00060 $installer->getConnection()->select()
00061 ->from($installer->getTable('catalog_category_entity'))
00062 ->order('level')
00063 ->order('path')
00064 );
00065
00066
00067
00068
00069 $installer->run("
00070 ;
00071 ");
00072
00073 foreach ($categories as $category) {
00074 $categoryId = $category['entity_id'];
00075 $query = "INSERT INTO `{$installer->getTable('catalog_category_product_index')}`
00076 SELECT DISTINCT {$categoryId}, `product_id`, `position`, {$categoryId}=`category_id` AS `is_parent`
00077 FROM `{$installer->getTable('catalog_category_product')}`
00078 WHERE `category_id` IN(
00079 SELECT `entity_id` FROM `{$installer->getTable('catalog_category_entity')}`
00080 WHERE `path` LIKE '{$category['path']}%'
00081 )
00082 GROUP BY `product_id`
00083 ORDER BY `is_parent` DESC";
00084 $installer->run($query);
00085 }
00086
00087 $installer->run("
00088 ;
00089 ");
00090
00091
00092
00093
00094
00095
00096
00097 $statusAttributeId = $installer->getAttributeId('catalog_product', 'status');
00098 $visibilityAttributeId = $installer->getAttributeId('catalog_product', 'visibility');
00099
00100 $installer->run("
00101 ;
00102 ");
00103 $websiteIds = $installer->getConnection()->fetchCol("SELECT website_id FROM {$installer->getTable('core_website')}");
00104 foreach ($websiteIds as $websiteId) {
00105 $storeIds = $installer->getConnection()->fetchCol("
00106 SELECT store_id FROM {$installer->getTable('core_store')} WHERE website_id={$websiteId}
00107 ");
00108 foreach ($storeIds as $storeId) {
00109 if (!$storeId) {
00110 continue;
00111 }
00112 $installer->run("
00113 INSERT INTO {$installer->getTable('catalog_product_enabled_index')}
00114 SELECT t_v_default.entity_id, {$storeId}, IFNULL(t_v.value, t_v_default.value)
00115 FROM {$installer->getTable('catalog_product_entity_int')} AS t_v_default
00116 INNER JOIN {$installer->getTable('catalog_product_website')} AS w ON w.product_id=t_v_default.entity_id AND w.website_id={$websiteId}
00117 LEFT JOIN {$installer->getTable('catalog_product_entity_int')} AS `t_v`
00118 ON (t_v.entity_id = t_v_default.entity_id) AND (t_v.attribute_id='{$visibilityAttributeId}') AND (t_v.store_id='{$storeId}')
00119 INNER JOIN {$installer->getTable('catalog_product_entity_int')} AS `t_s_default`
00120 ON (t_s_default.entity_id = t_v_default.entity_id) AND (t_s_default.attribute_id='{$statusAttributeId}') AND t_s_default.store_id=0
00121 LEFT JOIN {$installer->getTable('catalog_product_entity_int')} AS `t_s`
00122 ON (t_s.entity_id = t_v_default.entity_id) AND (t_s.attribute_id='{$statusAttributeId}') AND (t_s.store_id='{$storeId}')
00123 WHERE
00124 t_v_default.attribute_id='{$visibilityAttributeId}' AND t_v_default.store_id=0
00125 AND (IFNULL(t_s.value, t_s_default.value)=".Mage_Catalog_Model_Product_Status::STATUS_ENABLED.")
00126 ");
00127 }
00128 }
00129
00130 $installer->run("
00131
00132 ");
00133
00134 $installer->run("
00135 ALTER TABLE `{$installer->getTable('core_url_rewrite')}` ADD INDEX `IDX_CATEGORY_REWRITE` (`category_id`, `is_system`, `product_id`, `store_id`, `id_path`);
00136 ");
00137
00138 $installer->endSetup();