Magento collection : field occurrence counting

Say I have the following entity called inventory:

I would like to know if there are database, sql or magento actions or any other methods to produce:

I have solved this by iterating through the entire collection and inserting into a temporary table : i.e

$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
}

I can then retrieve what I want. I don't see an issue with it, but my entity is a lot larger than the example as well as the set of data containing anything between 2000 - 15 000 rows. is there not a more efficient way of doing this?

EDIT: In words, I would like to search the collection for occurrences of the "sku" field and return an array of unique sku's and the number of times it was found in the initial collection.

Answers


To get a particular set of column values from a collection you can use the getColumnValues() method.

For example, using a Magento product collection, this would return an array containing the sku attribute value for every product:

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

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

Finally, to answer the second part of your question, to count the occurence of each unique value:

array_count_values($skus);

This will give you a nice associative array of sku => occurrence count.


Need Your Help

jQuery Mouseover Event is Duplicating

javascript jquery html css iframe

I'm pretty new to this. I'm trying to trigger a preview of a link via an iFrame, triggered by a hover (mouseover) event.

Play audio with Python

python audio

How can I play audio (it would be like a 1 second sound) from a Python script?