如何实现setPagesize的分页,现在返回所有产品

时间:2013-05-05 04:47:21

标签: magento

所有我想在集合上设置setPageSize(),以便我可以将其用于分页。

无论我在setPageSize中放入什么整数,我都会收到所有产品。

代码:

    <?php

class Rik_Featured_Block_Featured extends
Mage_Core_Block_Template {

    private $_itemPerPage = 2;
    public $_category_id = '' ;
    private $_currentPage = '';

    public function __construct() {
        $custom_var_code = Mage::getModel('core/variable')->loadByCode('homepage_firstrow_prod');
        if(isset($custom_var_code)) echo $this->_category_id = $custom_var_code->getValue('text') ;
        echo $page_var = $this->getRequest()->getParam('p');
        if(isset($page_var)&&$page_var!=0){           
            $this->_currentPage = $page_var;
        }
        if(isset($page_var)&&$page_var == '0'){           
            $this->_currentPage = '1';
        }


    }


    /****************** Here is my setcurpage and setpagesize************************/

        public function allProducts() {
        $collection = $this->_getCollection();
        echo 'col is'.$collection->count();
        /*Setting current page and page size */
        $collection->setCurPage(1);
        $collection->setPageSize($this->_itemPerPage);
        return $collection;
    }

        private function _getCollection() {

        $category = Mage::getModel('catalog/category')->load($this->_category_id);

        if($category->getUrlPath() != NULL) {

            $collection = $category->getProductCollection();


            //Mage::helper('catalog/product')->setSkipSaleableCheck(true);
            $collection->addWebsiteFilter();
            $collection->addUrlRewrite($this->_category_id);
            $collection->addMinimalPrice()->addFinalPrice()->addTaxPercents();
            Mage::getSingleton('catalog/product_visibility')
                    ->addVisibleInCatalogFilterToCollection($collection);
            Mage::getSingleton('catalog/product_status')
                    ->addVisibleFilterToCollection($collection);
            $collection->addAttributeToSelect(array('entity_id', 'sku', 'name', 'short_description', 
                'description', 'price', 'thumbnail', 'image', 'url_path', 'type_of'));

           return $collection;
        }
        else { echo 'cat does not exit';}
      }




    public function totalPages() {       

        $_collections_count = $this->_getCollection()->count();
        return $number_of_pages = ceil($_collections_count / $this->_itemPerPage);
    }



}

使用__constructor也是我在这个块中使用的方式一个好主意吗?

谢谢。

1 个答案:

答案 0 :(得分:8)

按相反的顺序:

首先,永远不要将PHP的__construct与Magento的块或模型一起使用,尤其是在不调用父__construct的情况下。请改用Magento的_construct(请注意下划线)

其次,您正在调用正确的分页方法。如果您稍微简化一下代码,

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

$collection->setCurPage(1);
$collection->setPageSize(10);

$i=1;
foreach($collection as $item)
{
    var_dump($i . ') ' . $item->getSku());
    $i++;
}

很容易看到setCurPagesetPageSize方法按预期工作。

//results of running above code
string '1) n2610' (length=8)
string '2) bb8100' (length=9)
string '3) sw810i' (length=9)
string '4) 8525PDA' (length=10)
string '5) MM-A900M' (length=11)
string '6) MA464LL/A' (length=12)
string '7) LX.FR206.001' (length=15)
string '8) VGN-TXN27N/B' (length=15)
string '9) M285-E' (length=9)
string '10) cn_3' (length=8)

问题是您致电count

$collection = $this->_getCollection();
echo 'col is'.$collection->count();
/*Setting current page and page size */
$collection->setCurPage(1);
$collection->setPageSize($this->_itemPerPage);
return $collection;

要计算产品集合,Magento必须加载整个集合,(加载逻辑对于简单的SELECT count(*)来说太复杂了)。因为您在之前调用了来设置页面信息,所以Magento会在知道页面限制之前加载完整的集合。

您可以通过在循环之前不调用countclear集合来解决此问题。使用和不使用$collection->clear();

,请尝试以下代码
$collection = Mage::getModel('catalog/product')
->getCollection();                      

$collection->load();
$collection->setCurPage(1);
$collection->setPageSize(10);   
$collection->clear();
$i=1;
foreach($collection as $item)
{
    var_dump($i . ') ' . $item->getSku());
    $i++;
}