gpt4 book ai didi

php - Magento 集合 : field occurrence counting

转载 作者:行者123 更新时间:2023-11-29 05:35:33 25 4
gpt4 key购买 nike

假设我有以下名为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com