Magento database related problems and solution


Problem :
Item (Mage_Catalog_Model_Product) with the same id "107" already exist
 
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id',  
       'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => 3,4)))
    ->addAttributeToSelect('*');
 
Solultion:
 
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 
        'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => 3,4)))
    ->addAttributeToSelect('*');
$collection->getSelect()->group('e.entity_id');