【问题标题】:Magento ordering collections after adding items添加项目后 Magento 订购集合
【发布时间】:2011-12-02 11:05:55
【问题描述】:

我有这个方法,我首先检索 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 方法,但它不起作用(IRC 上的 soemone 告诉我即 setOrder 修改查询)。所以我可以手动完成,但在添加项目后没有订购集合对我来说似乎很奇怪。我查看了 Varien_Data_Collection API,但没有看到任何可以帮助我的东西。我也尝试将集合类更改为 Varien_Data_Collection_Db 并设置 addOrder() 方法,但没有任何改变。

有什么想法吗?

谢谢!

卢克

【问题讨论】:

    标签: magento-1.4 magento-1.5 magento


    【解决方案1】:

    你可以打电话

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

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

    【讨论】:

    • 在调用 clear() 之后,当您说“在这里添加一些登录以进行订购”时,您是否还表示 setOrder()?我浏览了 Varien_Data_Collection (Collection.php) 的代码,我看到清楚地删除了所有项目 ($this->_items = array();) ,所以最初没有考虑它。
    • 是的,但是你可以点击指定的链接,也许在那里你会找到你需要的。
    猜你喜欢
    • 2015-05-09
    • 2023-03-27
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    相关资源
    最近更新 更多