Magento在添加项目后订购集合

Magento在添加项目后订购集合

本文介绍了Magento在添加项目后订购集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have this method on which i retrieve first a Varien_Data_Collection and then I add items one by one from another Varien_Data_Collection by addItem():

    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方法,但它不起作用(IRC上的soemone告诉我该setOrder修改查询).因此我可以手动完成,但对我来说奇怪的是,添加项目后没有订购一个收藏夹.我看了看Varien_Data_Collection API,但看不到有什么对我有帮助的.我还尝试过将集合类更改为Varien_Data_Collection_Db并设置addOrder()方法,但没有任何改变.

So when i have the final collection i want to set the order since items in it have one attribute in common ('add_date'), so i set the setOrder methos, but it doesn't work (soemone on IRC told me that setOrder modify the query). so I can do it manually but it seems strange to me that there's not to order a collection after adding items. i took a look on Varien_Data_Collection API but I don't see anything that could help me. I've also tried to change the collection class to Varien_Data_Collection_Db and setting the addOrder() method but nothing has changed.

有什么主意吗?

谢谢!

卢克

推荐答案

您可以致电

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

OR

您可以具有某种排序功能,例如在最后一条消息中指定的此处

You can have some kind of sorting function like specified here in the last message.

这篇关于Magento在添加项目后订购集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:36