Magento Product Collection – Get All Products by Category ID

magento-1.9product-collection

I'm trying to get product collection and join table catalog/category_product.

$productCollection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect('*')
                        ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
                        ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left');

And I have the error.
Item (Mage_Catalog_Model_Product) with the same id "338" already exist.
What am I doing wrong?
Thanks.

Best Answer

Please use this collection.

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToSelect('*');