php - Magento 集合 : field occurrence counting

标签 php mysql magento

假设我有以下名为 inventory 的实体:

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 - 15 000 行之间任何内容的数据集大得多。没有更有效的方法吗?

编辑:换句话说,我想在集合中搜索“sku”字段的出现,并返回一组唯一的 sku 以及它在初始集合中找到的次数。

最佳答案

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

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

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

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

最后,回答问题的第二部分,计算每个唯一值的出现次数:

array_count_values($skus);

这将为您提供一个很好的 sku 关联数组 => 出现次数。

关于php - Magento 集合 : field occurrence counting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11020113/

相关文章:

mysql - 如何将 MQTT Mosquitto 发布事件存储到 MySQL 中?

Magento:为所有 child 阻止unsetChild吗?

php - Composer - 如何忽略一些 map 文件?

php - 使用终端在MySQL中创建的数据库与PhpMyAdmin中显示的数据库是否不同?

php - 如何修复从 HTML 中提取的纯文本的句子间距?

sql - 连接查询是否正确?

attributes - Magento - 按类别获取可过滤的属性

javascript - 我的jquery ajax代码无故刷新页面

php - 亚马逊 EC2, Elastic Beanstalk : My images disappear

mysql 没有给出想要的结果