Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento 如何为分页实现setPagesize,现在返回所有产品_Magento - Fatal编程技术网

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

Magento 如何为分页实现setPagesize,现在返回所有产品,magento,Magento,我所要做的就是在集合上设置PageSize,以便我可以使用它进行分页 无论我在setPageSize中输入了什么整数,我都会返回所有产品 代码: 另外,像我在这个块中使用的那样使用_构造函数是个好主意吗 谢谢。按相反顺序: 首先,永远不要将PHP的_构造用于Magento的块或模型,尤其是在没有调用父_构造的情况下。改用Magento的_构造。注意下划线 其次,您正在调用正确的分页方法。如果你把代码简化一点 $collection = Mage::getModel('catalog/produc

我所要做的就是在集合上设置PageSize,以便我可以使用它进行分页

无论我在setPageSize中输入了什么整数,我都会返回所有产品

代码:

另外,像我在这个块中使用的那样使用_构造函数是个好主意吗

谢谢。

按相反顺序:

首先,永远不要将PHP的_构造用于Magento的块或模型,尤其是在没有调用父_构造的情况下。改用Magento的_构造。注意下划线

其次,您正在调用正确的分页方法。如果你把代码简化一点

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

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

$i=1;
foreach($collection as $item)
{
    var_dump($i . ') ' . $item->getSku());
    $i++;
}
很容易看到setCurPage和setPageSize方法按预期工作

//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)
问题是你要数数

要对产品集合进行计数,Magento必须加载整个集合,加载逻辑对于简单的选择计数*来说太复杂。因为您在设置页面信息之前调用了count,所以Magento会在意识到页面限制之前加载完整的集合

您可以通过不调用count或在循环之前清除集合来修复此问题。尝试以下代码,包括$collection->clear和$collection->clear

按相反顺序:

首先,永远不要将PHP的_构造用于Magento的块或模型,尤其是在没有调用父_构造的情况下。改用Magento的_构造。注意下划线

其次,您正在调用正确的分页方法。如果你把代码简化一点

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

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

$i=1;
foreach($collection as $item)
{
    var_dump($i . ') ' . $item->getSku());
    $i++;
}
很容易看到setCurPage和setPageSize方法按预期工作

//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)
问题是你要数数

要对产品集合进行计数,Magento必须加载整个集合,加载逻辑对于简单的选择计数*来说太复杂。因为您在设置页面信息之前调用了count,所以Magento会在意识到页面限制之前加载完整的集合

您可以通过不调用count或在循环之前清除集合来修复此问题。尝试以下代码,包括$collection->clear和$collection->clear


正如@Alan提到的,$collection->count将加载所有项目并进行计数。要获取集合的记录数,应该使用$collection->getSize$收集->获取大小使用选择计数*。你又一次救了我的命艾伦;好小费!正如@Alan提到的,$collection->count将加载所有项目并进行计数。要获取集合的记录数,应该使用$collection->getSize$收集->获取大小使用选择计数*。你又一次救了我的命艾伦;好小费!
$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++;
}