1

我试图在集合上设置页面大小(),以便我可以将它用于分页。

无论我将什么整数放入 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 也是一个好主意吗?

谢谢你。

4

1 回答 1

9

以相反的顺序:

首先,永远不要将 PHP__construct与 Magento 的块或模型一起使用,尤其是在不调用 parent 的情况下__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(*))。因为您在设置页面信息之前调用了 count ,所以 Magento 在它知道页面限制之前会加载一个完整的集合。

您可以通过在循环之前不调用countclearing 集合来解决此问题。尝试以下代码,无论有无$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++;
}
于 2013-05-05T06:57:57.147 回答