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 $select = $installer->getConnection()->select()
00033 ->from($installer->getTable('catalog/category_product_index'), array(
00034 'category_id' => 'category_id',
00035 'product_id' => 'product_id',
00036 'is_parent' => 'is_parent',
00037 'store_id' => 'store_id',
00038 'rows_count' => 'COUNT(*)'))
00039 ->group(array('category_id' , 'product_id' , 'is_parent' , 'store_id'))
00040 ->having('rows_count > 1');
00041 $query = $installer->getConnection()->query($select);
00042
00043 while ($row = $query->fetch()) {
00044 $sql = 'DELETE FROM `' . $installer->getTable('catalog/category_product_index') . '`'
00045 . ' WHERE category_id=? AND product_id=? AND is_parent=? AND store_id=?'
00046 . ' LIMIT ' . ($row['rows_count'] - 1);
00047 $installer->getConnection()->query($sql, array(
00048 $row['category_id'],
00049 $row['product_id'],
00050 $row['is_parent'],
00051 $row['store_id']
00052 ));
00053 }
00054
00055 $installer->getConnection()->dropKey(
00056 $installer->getTable('catalog/category_product_index'),
00057 'UNQ_CATEGORY_PRODUCT'
00058 );
00059 $installer->getConnection()->addKey(
00060 $installer->getTable('catalog/category_product_index'),
00061 'UNQ_CATEGORY_PRODUCT',
00062 array('category_id', 'product_id', 'is_parent', 'store_id'),
00063 'unique'
00064 );
00065
00066 $installer->endSetup();