Collections Magento:如何将两个产品集合合并为一个?
如果我有两个产品集合,有没有办法将它们合并为一个 例如(我的最终目的并不是实际收集2只猫,这只是为了说明问题):Collections Magento:如何将两个产品集合合并为一个?,collections,magento,Collections,Magento,如果我有两个产品集合,有没有办法将它们合并为一个 例如(我的最终目的并不是实际收集2只猫,这只是为了说明问题): 任何帮助都将不胜感激 我认为没有这样的方法,但您可能可以这样做: foreach ($collection1 as $item){ $collection2->addElem($item); } 您可以不使用2直接筛选收藏 $products = Mage::getModel('catalog/product'); $_collection = $products-&
任何帮助都将不胜感激 我认为没有这样的方法,但您可能可以这样做:
foreach ($collection1 as $item){
$collection2->addElem($item);
}
您可以不使用2直接筛选收藏
$products = Mage::getModel('catalog/product');
$_collection = $products->getCollection();
->addCategoryFilter(2)
->load();
或者尝试使用“addItem”将结果添加到集合中。另请参见中Magento中几乎每个(或每个?)集合都继承自。集合是一种特殊的对象,用于保存其他类型的对象。没有合并集合的方法,但可以向集合中添加适当类型的其他项
这样的代码应该可以让您到达您想要去的地方,尽管可能有更有效的方法来循环和执行实际的合并
$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection();
$collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection();
//load an empty collection (filter-less collections will auto-lazy-load everything)
$merged = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id',-1);
//add items from the first collection
foreach($collection1 as $item)
{
$merged->addItem($item);
}
//add items from the second collection
foreach($collection2 as $item)
{
//magento won't let you add two of the same thing to a collection
//so make sure the item doesn't already exist
if(!$merged->getItemById($item->getId()))
{
$merged->addItem($item);
}
}
//lets make sure we got something
foreach($merged as $product)
{
var_dump($product->getName());
}
假设要组合在一起的项目属于同一类型,并且存在于数据库中,则可以执行以下操作:
$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection();
$collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection();
$merged_ids = array_merge($collection1->getAllIds(), $collection2->getAllIds());
// can sometimes use "getLoadedIds()" as well
$merged_collection = Mage::getResourceModel('catalog/product_collection')
->addFieldToFilter('entity_id', array('in' => $merged_ids))
->addAttributeToSelect('*');
在这里,我知道要按
entity\u id
进行过滤,因为这是产品的关键字段,就像大多数实体类型一样,一些平面表具有不同的主键。通常,您可以使用集合的getIdFieldName()
方法之一对其进行推广。在这种情况下,产品是一个不好的例子,因为它的ID字段名没有正确填写。对于多个类别的产品集合,可以使用以下代码
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku');
$collection->getSelect()
->join(
'catalog_category_product',
'product_id=entity_id',
array('category_id')
)
->where('catalog_category_product.category_id IN (?)', $categories);
我无法筛选,因为我将以两种不同的方式获取集合,并且无法向其中添加一个筛选器。我以前试过添加项,但当我把它放到左侧导航和列表页面时,计数都搞乱了。哦,对不起,我不知道。您是否尝试过不合并两个集合,而是根据以前集合的数据创建第三个集合?(例如id)这需要3个数据库查询,但haltabush解决方案只需要2个数据库查询。如果合并的集合不需要额外的过滤器或排序,我就不会使用它。一个旁注
addFieldToFilter($entity_id',$merged_id)
将转换为其中(entity_id=1001)或(entity_id=1002)或(entity_id=1003)或(entity_id=1004)或……
比[code>addFieldToFilter($entity_id',array($merged)中的慢
这里的问题是,您可能会遇到例外情况:具有相同id“***”的项目(Mage\u Catalog\u Model\u Product)已经存在
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku');
$collection->getSelect()
->join(
'catalog_category_product',
'product_id=entity_id',
array('category_id')
)
->where('catalog_category_product.category_id IN (?)', $categories);