我遇到一个奇怪的问题,即当按类别ID过滤时,产品集合不会返回任何产品,并且应将结果中包含的产品分配给多个类别。在这里。
产品ABC
已分配到类别91
和558
。
这是Magento代码(实际代码):
$categoryIds = array(91,369);
$collection = mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array('name','price','special_price','sku','small_image'))
->joinField('category_id', // add category ID field to collection
'catalog_category_product',
'category_id',
'product_id=entity_id',
null,
'left')
->addAttributeToFilter('category_id', array('in' => $categoryIds));
对应查询
SELECT `e`.*, `at_category_id`.`category_id`
FROM `catalog_product_entity` AS `e`
LEFT JOIN `catalog_category_product` AS `at_category_id`
ON (at_category_id.`product_id`=e.entity_id)
WHERE (at_category_id.category_id IN(91, 369));
这会返回产品ABC
。
现在,ABC
分为两类,如开头所述。因此,如果我设置$categoryIds = array(91,558)
,它应该...WHERE (at_category_id.category_id IN(91, 558))
并返回ABC
两次。但是..发生某种错误(未记录)并且不返回任何内容。由于某些原因没有记录日志,我不知道问题是什么。然后,我添加了->group(array('sku'))
并且它可以工作并返回ABC
一次..(如果按其他列分组,则无效)。为什么会这样?
我想我在某处读过产品系列不会让你有重复的实体ID ..在这种情况下它会有意义。但为什么没有错误?