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
00032 $categoryIndexTable = $installer->getTable('catalog/category_product_index');
00033
00034 $installer->getConnection()->addColumn(
00035 $categoryIndexTable,
00036 'store_id',
00037 'smallint(5) unsigned NOT NULL default \'0\''
00038 );
00039
00040 $installer->getConnection()->addColumn(
00041 $categoryIndexTable,
00042 'visibility',
00043 'tinyint(3) unsigned NOT NULL'
00044 );
00045
00046
00047
00048
00049
00050 $installer->getConnection()->delete($categoryIndexTable, 'category_id='.Mage_Catalog_Model_Category::TREE_ROOT_ID);
00051 $installer->getConnection()->addKey(
00052 $categoryIndexTable,
00053 'FK_CATALOG_CATEGORY_PRODUCT_INDEX_CATEGORY_ENTITY',
00054 array('category_id')
00055 );
00056 $installer->getConnection()->dropKey($categoryIndexTable, 'IDX_CATEGORY_POSITION');
00057 $installer->getConnection()->dropKey($categoryIndexTable, 'UNQ_CATEGORY_PRODUCT');
00058
00059 $storesData = $installer->getConnection()->fetchAll("
00060 SELECT
00061 s.store_id, s.website_id, c.path AS root_path
00062 FROM
00063 {$installer->getTable('core/store')} AS s,
00064 {$installer->getTable('core/store_group')} AS sg,
00065 {$installer->getTable('catalog/category')} AS c
00066 WHERE
00067 sg.group_id=s.group_id
00068 AND c.entity_id=sg.root_category_id
00069 ");
00070
00071 foreach ($storesData as $storeData) {
00072 $storeId = $storeData['store_id'];
00073 $websiteId = $storeData['website_id'];
00074 $path = $storeData['root_path'];
00075
00076 $query = "INSERT INTO {$categoryIndexTable}
00077 (`category_id`, `product_id`, `position`, `is_parent`, `store_id`, `visibility`)
00078 SELECT
00079 ci.category_id,
00080 ci.product_id,
00081 ci.position,
00082 ci.is_parent,
00083 {$storeId},
00084 ep.visibility
00085 FROM
00086 $categoryIndexTable AS ci
00087 INNER JOIN {$installer->getTable('catalog/product_website')} AS pw
00088 ON pw.product_id=ci.product_id AND pw.website_id={$websiteId}
00089 INNER JOIN {$installer->getTable('catalog/category')} AS c
00090 ON c.entity_id=ci.category_id AND c.path LIKE '{$path}%'
00091 INNER JOIN {$installer->getTable('catalog/product_enabled_index')} AS ep
00092 ON ep.product_id=ci.product_id AND ep.store_id={$storeId}
00093 WHERE
00094 ci.store_id=0";
00095
00096 $installer->run($query);
00097 }
00098
00099 $installer->getConnection()->delete($categoryIndexTable, 'store_id=0');
00100
00101 $installer->getConnection()->addKey(
00102 $categoryIndexTable,
00103 'UNQ_CATEGORY_PRODUCT',
00104 array('store_id', 'category_id', 'product_id')
00105 );
00106
00107 $installer->getConnection()->addKey(
00108 $categoryIndexTable,
00109 'IDX_JOIN',
00110 array('product_id', 'store_id', 'category_id', 'visibility')
00111 );
00112
00113 $installer->getConnection()->addKey(
00114 $categoryIndexTable,
00115 'IDX_BASE',
00116 array('store_id', 'category_id', 'visibility', 'is_parent', 'position')
00117 );
00118
00119 $installer->getConnection()->addConstraint(
00120 'FK_CATEGORY_PRODUCT_INDEX_STORE',
00121 $categoryIndexTable,
00122 'store_id',
00123 $installer->getTable('core/store'),
00124 'store_id'
00125 );
00126
00127 $installer->endSetup();