Php 筛选两个类别的产品集合Magento 1.7

Php 筛选两个类别的产品集合Magento 1.7,php,magento,collections,magento-1.7,Php,Magento,Collections,Magento 1.7,我想获得一个包含a类或B类产品的产品集合。我已经能够通过以下php代码成功获得这些产品: $collection = Mage::getModel('catalog/product') ->getCollection() ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left') ->addAtt

我想获得一个包含a类或B类产品的产品集合。我已经能够通过以下php代码成功获得这些产品:

$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' => 4,19)))
    ->addAttributeToSelect('*');
但是,如果产品同时属于类别4和类别19,则会显示错误:

Item (Mage_Catalog_Model_Product) with the same id "173" already exist
这是因为集合中有一个重复的行。我正在努力找到正确的代码来过滤掉集合中的任何重复行。解决方案必须是将值分组,或使用distinct,但我不确定如何继续


另请参见

好的,多亏了


group子句可以克服返回的重复产品id。

相同的错误链接建议

当您在集合中具有相同的id时,将发生这种情况

所以我在收集的最后使用了下面的代码

$collection->getSelect()->distinct(true)


这将使select变得不同

我收到了这个错误,Magento通过“/var/reports/xxx”报告的内容是:

a:5:{i:0;s:71:"Item (Mage_Catalog_Model_Product) with the same id "xxx"
我想到的是停用这个id的产品,它是固定的。然后我删除了这个产品并重新创建它。这不是一个完美的解决方案,但目前有效。
但我仍然想知道那里有什么问题?在我们的例子中,这个错误来得突然。我们最近没有改变或发展出任何新的东西,这都是我们的错。有人知道这件事为什么会突然发生吗

使用多个类别ID筛选产品集合

$all_categories = array('3','13','113');   
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 
                    'product_id = entity_id', null, 'left')
                  ->addAttributeToSelect('*')
                  ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                  ->addAttributeToFilter('category_id', array($all_categories));
foreach($productCollection as $product)
{
    echo $product->getId() .$product->getName() . "<br/>";
}
$all_categories=数组('3','13','113');
$productCollection=Mage::getModel('catalog/product')->getCollection();
$productCollection->joinField('category\u id'、'category/category\u product'、'category\u id',
“产品标识=实体标识”,空,“左”)
->addAttributeToSelect(“*”)
->addAttributeToFilter('type_id',array('eq'=>'simple'))
->addAttributeToFilter('category_id',数组($all_categories));
foreach($productCollection作为$product)
{
echo$product->getId().$product->getName().“
”; }
这对我来说是固定的
$collection->distinct(true)$all_categories = array('3','13','113');   
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 
                    'product_id = entity_id', null, 'left')
                  ->addAttributeToSelect('*')
                  ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                  ->addAttributeToFilter('category_id', array($all_categories));
foreach($productCollection as $product)
{
    echo $product->getId() .$product->getName() . "<br/>";
}