我想要获取包含 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
这是因为集合中有重复的行。我正在努力寻找正确的代码来过滤掉集合中的任何重复行。解决方案必须是将值分组,或使用不同的值,但我不知道如何继续。
也可以看看使用不同的方法过滤 Magento 集合而不是产品 https://stackoverflow.com/questions/4511314/filter-magento-collection-but-not-products-using-distinct
好的,我已经解决了这个问题,感谢https://stackoverflow.com/a/13291759/991491 https://stackoverflow.com/a/13291759/991491
$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');
group 子句可以解决返回的重复产品 ID 问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)