2012-06-13 36 views
1

说我有所谓的库存下列实体:Magento的集合:场次数计数

enter image description here

我想知道是否有数据库,SQL或Magento的行为或任何其他方法来产生:

enter image description here

我已经通过整个集合遍历并插入到一个临时表解决了这个:即

$inventorySet = Mage::getModel('custom/module')->getCollection(*); 
foreach($inventorySet as $item) 
{ 
    $this->insertItem($item->getSku()); 
} 

}

public function insertItem($sku) 
{ 
    //insert sku if it does not exist in the temp set 
    // if it does exists add one to the QTY field 
} 

然后我就可以找回我想要什么。我没有看到它的问题,但我的实体比示例以及包含2000-15000行之间的任何内容的数据集大得多。有没有更有效的方法来做到这一点?

欢呼声。

编辑:在单词中,我想搜索“sku”字段出现的集合,并返回一个不明确的sku数组以及它在初始集合中发现的次数。

thnks

回答

3

从集合中获得一组特定的列值可以使用getColumnValues()方法。

例如,使用Magento的产品集合,这将返回一个包含SKU属性值对每个产品的数组:

$collection = Mage::getResourceModel('catalog/product_collection') 
    ->addAttributeToSelect('sku'); 

$skus = $collection->getColumnValues('sku'); 

最后,回答你问题的第二部分,算的发生每一个独特的价值:

array_count_values($skus);

这会给你一个SKU =>出现次数的一个很好的关联数组。

+0

辉煌!非常感谢,正是我想要的。从来没有这样做过。干杯! – activeDev