2012-06-13 41 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