2015-06-01 33 views
1

我开发一个模块列出部分设备及正在运行到一个产品收集问题,出现以下错误:Magento的:允许重复的产品系列

项目(Mage_Catalog_Model_Product)具有相同的ID“1150”已经存在

我的问题的独特之处在于,我在WANT的产品不止一次收藏,因为我列出了所有适合特定机器的产品,并且一些产品(轴承)将适合不同机器地方。

我将附件信息附加到收藏品上,以便每件产品都会显示它在机器上的位置并首先按装修进行分类,但重要的是要将产品分别列入其中包含的每个附件。

但是,Magento不喜欢这个,我一直没有能够想出一个解决方案。我跟踪误差的lib \瓦瑞恩\ DATA \ Collection.php

 if (isset($this->_items[$itemId])) { 
      throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist'); 
     } 

由此我看到,该产品被设置成使用id作为数组键的排列,所以我不知道是否有避免在我的收藏中多次使用产品的好方法。

编辑: 这是我的代码来加载集合问。它适用于大多数选择,所以我没有问题的代码获得正确的产品,但宁可一些选择导致相同的产品不止一次列在集合,我想发生,但Magento不喜欢

$layer = Mage::getSingleton('catalog/layer'); 
    $layer->setCurrentCategory(3); 
    $collection = $layer->getProductCollection(); 

    $atvvalues = $session->getData($name); 

    $atv_id = $db->select() 
     ->from(array('fm'=>$collection->getTable('mcdfinder/vehicle')), 'id') 
     ->where('fm.make = ? ', $atvvalues['make']) 
     ->Where('fm.description = ?', $atvvalues['model']) 
     ->Where('fm.year = ?', $atvvalues['year']); 

    $collection->getSelect()->join(array('mmy' => $collection->getTable('mcdfinder/product')), 'mmy.product_id=e.sku', array('mmy.*')) 
     ->where('mmy.vehicle_id = ?', $atv_id); 

    $collection->getSelect()->join(array('atvcat' => $collection->getTable('mcdfinder/category')), 'atvcat.id=mmy.category_id', array('atvcat.*')); 

    $collection->getSelect()->order(array('atvcat.category_gparent', 'atvcat.category_parent', 'atvcat.category_name', 'e.sku')); 

所以,我可能会与“差”的atvcat产品BG1,也与“前轮”的atvcat一个BG1,所以我需要它的有序集合的不同地方出现两次

+0

你可以发表你如何加载你的收藏的代码? –

回答

0

所以我通过覆盖lib \ Varien \ Data \ Collection.php在我的本地文件夹中得到了这个工作。

我改变了第一片断中的问题这样:

 if (isset($this->_items[$itemId])) { 
      $this->_items[] = $item; 
      //throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist'); 
     } 

这一行 “$这 - > _项[] = $项;”借用_addItem的逻辑,其中没有id的项目被添加到集合中(因为之前的问题是具有相同ID的重复项目之间的冲突)。

我很想听听为什么这是一个糟糕的解决方案(除了直接覆盖Varien)或者可能会导致问题,但到目前为止事情都很完美。

+0

不好的部分是抛出异常是有原因的。在此级别注释代码可能会导致其他代码按预期停止工作,因为应该引发这些异常**并且应该引发**。我现在处于类似的情况。注释掉代码是一种很好的方法,但是我会建议为您的案例重写集合类,并且仅针对特定集合进行更改。 –