【问题标题】:Magento : merge two collections and keep the same sortingMagento:合并两个集合并保持相同的排序
【发布时间】:2025-11-24 12:20:05
【问题描述】:

有人可以帮帮我吗?

我有两个集合,我想将它们合并为一个保持相同排序的集合:

$collection_with_image = Mage::getModel('catalog/product')->getCollection()
    ->addCategoryFilter($category)
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('small_image', array('like' => '/%/%/%'))
    ;

$collection_basique_without_image = Mage::getModel('catalog/product')->getCollection()
    ->addCategoryFilter($category)
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(
        array('attribute' => 'small_image', 'null' => true),
        array('attribute' => 'small_image', 'eq' => 'no_selection')
        ), 
        '', 
        'left')
    ;   

我已经尝试过this solution,但它并没有保持相同的排序:/

我想要的最终结果是,只有一个集合如何在第一个位置包含带有图片的产品。

谢谢大家

【问题讨论】:

    标签: sorting magento collections merge


    【解决方案1】:
    $collection_with_image = Mage::getModel('catalog/product')->getCollection()
    ->addCategoryFilter($category)
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(
        array('attribute' => 'small_image', 'null' => true),
        array('attribute' => 'small_image', 'eq' => 'no_selection'),
        array('attribute' => 'small_image', 'like' => '/%/%/%')       
        ), 
    

    );

    这将输出格式如下的 WHERE 子句:

    WHERE ((small_image LIKE 'value') OR (small_image eq 'no_selection') OR (small_image eq 'null'))
    

    【讨论】:

    • tnx 为您解答,但它不会保持我想要的排序(带有图像的产品:/)