Gee*_*ide 10 collections magento
如果我有两个产品系列,有没有办法将它们合并为一个?
例如(我的最终目的不是实际上只获得2只猫的集合,这只是为了说明问题):
$collection1 = Mage::getModel('catalog/category')->load(148)->getProductCollection();
$collection2 = Mage::getModel('catalog/category')->load(149)->getProductCollection();
$merged_collection = merge_collections($collection1,$collection2);
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
clo*_*eek 23
假设您希望组合在一起的项目具有相同的类型并存在于数据库中,那么您可以执行以下操作:
$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('*');
Run Code Online (Sandbox Code Playgroud)
在这里我知道要过滤entity_id因为这是产品的关键字段,就像大多数实体类型一样,一些平面表具有不同的主键.通常,您可以使用集合的getIdFieldName()方法之一来概括它.在这种情况下,产品是一个不好的例子,因为它的ID字段名称没有正确填写.
Ala*_*orm 15
几乎Magento中的每个(或每个?)集合都继承了Varien Data Collection.集合是一个保存另一种类型对象的特殊对象.没有合并集合的方法,但您可以向集合中添加适当类型的其他项.
像这样的代码可以让你到达你想去的地方,虽然可能有更有效的方法来循环并进行实际的合并.
$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());
}
Run Code Online (Sandbox Code Playgroud)
我认为没有这种方法,但是您可以执行以下操作:
foreach ($collection1 as $item){
$collection2->addElem($item);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22752 次 |
| 最近记录: |