addAttributeToSort()在magento Product collection中不起作用?

时间:2012-10-16 10:40:07

标签: php magento

我在magento的products表中添加了自定义字段。我想排序使用该自定义字段,但它不会。我不知道是什么问题?。

    $_cproduct = Mage::getmodel('catalog/product');
    $_productCollection = $_cproduct->getCollection();
    $_productCollection->addAttributeToSelect('name')->addAttributeToSelect('expired')->addAttributeToSelect('dealcategory')->addAttributeToSelect('dealcity')->addAttributeToSelect('deal_position')->addAttributeToSelect('special_to_date')->addAttributeToSelect('title')->setStore(Mage::app()->getStore()->getId())->addStoreFilter(Mage::app()->getStore()->getId())->addAttributeToSort('entity_id', 'DESC')->addAttributeToSort('dealcategory', 'DESC');
    $_productCollection->addAttributeToFilter('status', 1)->addAttributeToFilter('expired', 0);


$_productCollection->addAttributeToFilter('dealcategory', array('in' => array(129, 135, 136, 137, 141, 156, 205)));


    $now = Mage::getModel('core/date')->timestamp(time());
    $dateStart = date('Y-m-d' . ' 00:00:00', $now);    

    $_productCollection->addFieldToFilter('special_to_date', array('from' => $dateStart));
    $dealcity = Mage::app()->getRequest()->getParam('dealcity', false);
    $_productCollection->addAttributeToSort('deal_position', 'ASC');


    echo "<pre>";
    print_r($_productCollection->getData());
    echo "</pre>";

但是每当我得到如下的异常订单时,我都希望根据“deal_position”进行排序

Array
(
    [0] => Array
        (
            [entity_id] => 2568
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => virtual
            [sku] => comedybar7/2/12
            [created_at] => 2012-07-02 18:36:22
            [updated_at] => 2012-10-16 09:52:41
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side travel deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 135
            [special_to_date] => 2012-10-25 00:00:00
            [deal_position] => 4
        )

    [1] => Array
        (
            [entity_id] => 2566
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => virtual
            [sku] => livingwellfinal
            [created_at] => 2012-06-28 14:48:34
            [updated_at] => 2012-10-16 09:51:58
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-11-30 00:00:00
            [deal_position] => 1
        )

    [2] => Array
        (
            [entity_id] => 2565
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => simple
            [sku] => laser-toenail-10-toes
            [created_at] => 2012-06-27 21:10:19
            [updated_at] => 2012-10-16 09:52:23
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-11-30 00:00:00
            [deal_position] => 3
        )

    [3] => Array
        (
            [entity_id] => 2564
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => simple
            [sku] => laser-toenail-5-toes
            [created_at] => 2012-06-27 21:10:19
            [updated_at] => 2012-10-16 09:52:10
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-12-31 00:00:00
            [deal_position] => 2
        )

)

我使用的addOrder()也返回相同的输出。有什么问题?

1 个答案:

答案 0 :(得分:2)

您似乎已经迷失在自己的代码中。您已添加 3 !在您的代码中排序:

line3: ...->addAttributeToSort('entity_id', 'DESC')->addAttributeToSort('dealcategory', 'DESC');
line10: $_productCollection->addAttributeToSort('deal_position', 'ASC');

确保您只需要排序。此外,不要多次使用addAttributeToSelect,使用一次,但将数组作为参数传递:

$_productCollection->addAttributeToSelect(array('name', 'expired', ...));