0

I have created a custom grid to show a monthly wise report, But the pagination is not working with the Varien_Data_Collection . Please find the code snipet below from Grid.php

      $rows  = $connection->fetchAll($sql);//this row will return an array

        $collection = new Varien_Data_Collection();

        foreach($rows as $row){
            $rowObj = new Varien_Object();
            $rowObj->setData($row);
            $collection->addItem($rowObj);
        }



        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
  • Why don't you define your collection as a resource model and get your collection in the standard Magento way for grid? – kiatng Apr 24 '18 at 8:16
  • I wrote an sql query because i need a monthly report. For more info please this question magento.stackexchange.com/questions/222793/… – Radhu Apr 24 '18 at 8:55
0

The (non-abstract) base-class Varien_Data_Collection - although it does have the setPageSize and setCurPage methods, those are not reflected within the aggregation of the Iterator:

class Varien_Data_Collection implements IteratorAggregate, Countable { ...

/**
 * Implementation of IteratorAggregate::getIterator()
 */
public function getIterator()
{
    $this->load();
    return new ArrayIterator($this->_items);
}

...

It will in any case return an ArrayIteator that has all objects. The load method doesn't change a thing here btw:

...

/**
 * Load data
 *
 * @return  Varien_Data_Collection
 */
public function loadData($printQuery = false, $logQuery = false)
{
    return $this;
}

...

/**
 * Load data
 *
 * @return  Varien_Data_Collection
 */
public function load($printQuery = false, $logQuery = false)
{
    return $this->loadData($printQuery, $logQuery);
}

...

The (non-abstract) base-class Varien_Data_Collection - although it does have the setPageSize and setCurPage methods, those are not reflected within the aggregation of the Iterator:

class Varien_Data_Collection implements IteratorAggregate, Countable { ...

/**
 * Implementation of IteratorAggregate::getIterator()
 */
public function getIterator()
{
    $this->load();
    return new ArrayIterator($this->_items);
}

...

It will in any case return an ArrayIteator that has all objects. The load method doesn't change a thing here btw:

...

/**
 * Load data
 *
 * @return  Varien_Data_Collection
 */
public function loadData($printQuery = false, $logQuery = false)
{
    return $this;
}

...

/**
 * Load data
 *
 * @return  Varien_Data_Collection
 */
public function load($printQuery = false, $logQuery = false)
{
    return $this->loadData($printQuery, $logQuery);
}

...

I'd say this qualifies as a bug, as the public interface makes one assume that the magent collection object Varien_Data_Collection provides pagination while it does not.

I have not searched for a bug-report regarding this issue. The solution is to use another iterator for pagination, for example like outlined in an answer to How to Paginate lines in a foreach loop with PHP with PHP's LimitIterator:

/**
 * Class Varien_Data_Collection_Pagination
 *
 * Aggregates missing Pagination on Collection on getting the Iterator
 *
 * @author hakre <http://hakre.wordpress.com/>
 */
class Varien_Data_Collection_Pagination implements IteratorAggregate
{
    /**
     * @var Varien_Data_Collection
     */
    private $collection;

    public function __construct(Varien_Data_Collection $collection)
    {
        $this->collection = $collection;
    }

    public function getIterator()
    {
        $collection = $this->collection;
        if (FALSE === $size = $collection->getPageSize()) {
            return $collection;
        }

        $page = $collection->getCurPage();
        if ($page < 1) {
            return $collection;
        }

        $offset = $size * $page - $size;

        return new LimitIterator(new IteratorIterator($collection), $offset, $size);
    }
}

Usage Example:

$collection = new Varien_Data_Collection();
$collectionIterator = new Varien_Data_Collection_Pagination($collection);

# [...] fill collection 

# set pagination:
$collection->setPageSize(3);
$collection->setCurPage(1);

echo iterator_count($collectionIterator); # 3 (int) 

Hope this will help you.

  • Actually i didn't understand anything, Sorry. What i have to do ? – Radhu Apr 24 '18 at 5:49
  • @Radhu this is the basic structure. You need to check example which I set in lastly. – Mayur Apr 24 '18 at 6:11
  • Varien_Data_Collection_Pagination , where we define this class ? – Radhu Apr 24 '18 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.