Magento在添加项目后订购集合

时间:2011-12-02 11:05:56

标签: magento-1.4 magento-1.5 magento

我有这个方法,我首先检索一个Varien_Data_Collection,然后我通过addItem()从另一个Varien_Data_Collection中逐个添加项目:

    protected function _prepareCollection(){

    $productId = $this->getRequest()->getParam('id');
    $websiteId = 0;
    if ($store = $this->getRequest()->getParam('store')) {
        $websiteId = Mage::app()->getStore($store)->getWebsiteId();

        $collection = Mage::getModel('productalert/stock')
                ->getCustomerCollection()
                ->join($productId, $websiteId); 

        foreach ($collection as $item) {
            $item->setData('is_customer', 'Sì');
        }


        $guestCollection = Mage::getModel('productsalert/gueststock')->getCollection()
                ->addFieldToFilter("product_id", $productId)
                ->addFieldToFilter("website_id", $websiteId);


        foreach ($guestCollection as $guestItem) {
            $obj = new Mage_Customer_Model_Customer();
            $obj->setData($guestItem->getData());
            $obj->setData('alert_stock_id', ($guestItem->getData('alert_stock_id')+100000000));
            $obj->setData('email', $guestItem->getData('guest_email'));
            $obj->setData('is_customer', 'No');
            $collection->addItem($obj);
        }
        $collection = $collection->setOrder('add_date','ASC');
        $this->_sortCollectionDescByDate($collection);
        $this->setCollection($collection);
    }
    else{
        $this->setCollection(new Varien_Data_Collection());
    }




    return parent::_prepareCollection();

}   

所以,当我有最终的集合时,我想设置顺序,因为其中的项目有一个共同的属性(' add_date'),所以我设置了setOrder方法,但它没有#t; t工作(IRC上的soemone告诉我setOrder修改了查询)。所以我可以手动完成,但是我觉得在添加项目后没有订购集合似乎很奇怪。我看了一下Varien_Data_Collection API,但是我没有看到任何可以帮助我的东西。我还尝试将集合类更改为Varien_Data_Collection_Db并设置addOrder()方法,但没有任何更改。

任何想法?

谢谢!

卢克

1 个答案:

答案 0 :(得分:1)

你可以打电话

$collection->clear();
$collection->....//here you add some logic for ordering;
$collection->load();//here collection with new filters will be loaded. 

OR

您可以在最后一条消息中使用某种排序功能,例如指定的here