Magento Special Collection Pagination

I created a collection by adding items to the 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 to the Magento pager block, as shown below, it breaks the pagination on my user page.

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

PS I have never had problems with collections extracted from model collections, such as Mage :: getModel ('module / modelname') β†’ getCollection (). These are simply collections created by adding elements to the Varien_Data_Collection object.

+6
source share
4 answers

The pager calls setPageSize in its collection, which - if you track it - is used only by getLastPageNumber . This means that the pager can show the number of pages accurately, but that is. This is Varien_Data_Collection_Db , which actually does something with the current page number and size, representing them as a LIMIT for SQL.

To create a collection that takes page criteria into account, you will need to create a descendant for the class. For ideas, see the Varien_Data_Collection_Filesystem source and how it implements loadData .


I just re-read your question and realized that you can do this:

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

This collection will be very happy.

+6
source

I just doubt it (maybe I'm wrong):

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

which should look like this:

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

Let me know if this works for you. Thanks

EDIT:
I finally figured it out. Here's 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); } 
+2
source

A 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
source

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; } } 
0
source

Source: https://habr.com/ru/post/906457/


All Articles