6
votes

I created a collection by adding items to a Varien_Data_Collection collection object.

$collection = new Varien_Data_Collection();
  foreach($array_of_products as $productId){
    $collection->addItem(Mage::getModel('catalog/product')->load($productId));
}

However when this object is passed on to Magento pager block as given below, it breaks the pagination in my custom page.

$pager = $this->getLayout()->createBlock('page/html_pager', 'retailerfe.analysis.pager')
            ->setCollection($collection);

P.S I have never had problems with collections fetched from model collections like Mage::getModel('module/modelname')->getCollection(). It is just collections created by adding items to a Varien_Data_Collection Object.

4
please post the text of the error message, and tell us what version of Magento. v1.4 changed the way that pagination works. - Jonathan Day
There is no error message. It is just that pagination becomes a little weird. The pager calculates the page numbers correctly. It seems like it is not able to apply limits on the collection. All the items show on all the pages. I use Magento v 1.5.0.1 - Adheesh
Seems like this behavior occurs whenever a collection created by adding items to an empty Varien_Data_Collection object is made to paginate. - Adheesh
Can you able to solve this issue? - Mufaddal

4 Answers

6
votes

The pager is calling setPageSize on your collection which - if you trace it - is only used by getLastPageNumber. This means the pager can show the number of pages accurately but that's it. It is Varien_Data_Collection_Db that actually does anything with the current page number and size by rendering them as a LIMIT clause for SQL.

To create a collection that respects the page criteria you will have to create a descendant to the class. For ideas look at the source of Varien_Data_Collection_Filesystem and how it implements loadData.


I've just re-read your question and realised you can do this:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addIdFilter($array_of_products);

This collection will page quite happily.

2
votes

I just doubt on(may be i am wrong):

$collection->addItem(Mage::getModel('catalog/product')->load($productId));

which should be like this:

$collection->addItem(Mage::getModel('catalog/product')->load($productId)->getData());

Let me know if that works for you. Thanks

EDIT:
Finally i figured it out. Here is how you should do it:

<?php
$collection = new Varien_Data_Collection();
foreach($array_of_products as $productId){
    $product = Mage::getModel('catalog/product')->load($productId); 
    $_rowObject = new Varien_Object();
    $_rowObject->setData($product->getData());
    $collection->addItem($_rowObject);
}
0
votes

Solution is provided.

class Test_Featuredsalons_Block_Featuredsalons extends Mage_Core_Block_Template
{

    public function __construct()
    {
        parent::__construct();
        $collection = Mage::getModel('featuredsalons/featuredsalons')->getCollection();
        $this->setCollection($collection);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
        $pager->setCollection($this->getCollection());
        $this->setChild('pager', $pager);
        $this->getCollection()->load();

        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    public function getCollection()     
     {            
        $limit      =   10;
        $curr_page  =   1;

        if(Mage::app()->getRequest()->getParam('p'))
        {
            $curr_page  =   Mage::app()->getRequest()->getParam('p');
        }
        //Calculate Offset  
        $offset     =   ($curr_page - 1) * $limit;

        $collection =   Mage::getModel('featuredsalons/featuredsalons')->getCollection()
                                                    ->addFieldToFilter('status',1);     
        $collection->getSelect()->limit($limit,$offset);

        return $collection;
    }   
}

Thanks, Kashif

0
votes

Use the following class to extend the Varien Data Collection class:

class Pageable_Varien_Data_Collection extends Varien_Data_Collection
{
    /**
     * Load data
     *
     * @param   bool $printQuery
     * @param   bool $logQuery
     *
     * @return  Pageable_Varien_Data_Collection
     */
    public function load($printQuery = false, $logQuery = false)
    {
        if ($this->isLoaded()) {
            return $this;
        }
        $this->_renderLimit();
        $this->_setIsLoaded();
        return $this;
    }

    /**
     * @return  Pageable_Varien_Data_Collection
     */
    protected function _renderLimit()
    {
        if ($this->_pageSize) {
            $currentPage = $this->getCurPage();
            $pageSize = $this->_pageSize;
            $firstItem = (($currentPage - 1) * $pageSize + 1);
            $lastItem = $firstItem + $pageSize;
            $iterator = 1;
            foreach ($this->getItems() as $key => $item) {
                $pos = $iterator;
                $iterator++;
                if ($pos >= $firstItem && $pos <= $lastItem) {
                    continue;
                }
                $this->removeItemByKey($key);
            }
        }
        return $this;
    }

    /**
     * Retrieve collection all items count
     *
     * @return int
     */
    public function getSize()
    {
        if (is_null($this->_totalRecords)) {
            $this->_totalRecords = count($this->getItems());
        }
        return intval($this->_totalRecords);
    }

    /**
     * Retrieve collection items
     *
     * @return array
     */
    public function getItems()
    {
        return $this->_items;
    }
}