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'), array(
00034 'category_id',
00035 'product_id',
00036 'position',
00037 'cnt' => 'COUNT(product_id)'
00038 ))
00039 ->group('category_id')
00040 ->group('product_id')
00041 ->having($installer->getConnection()->quoteInto('cnt > ?', 1));
00042 $rowSet = $installer->getConnection()->fetchAll($select);
00043
00044 foreach ($rowSet as $row) {
00045 $data = array(
00046 'category_id' => $row['category_id'],
00047 'product_id' => $row['product_id'],
00048 'position' => $row['position']
00049 );
00050 $installer->getConnection()->delete($installer->getTable('catalog_category_product'), array(
00051 $installer->getConnection()->quoteInto('category_id = ?', $row['category_id']),
00052 $installer->getConnection()->quoteInto('product_id = ?', $row['product_id'])
00053 ));
00054 $installer->getConnection()->insert($installer->getTable('catalog_category_product'), $data);
00055 }
00056
00057 $installer->run("
00058 ALTER TABLE `{$installer->getTable('catalog_category_product')}`
00059 ADD UNIQUE `UNQ_CATEGORY_PRODUCT` (`category_id`, `product_id`);
00060 ");
00061
00062 $installer->endSetup();