Magento自定义集合中断分页
我通过向Varien_Data_collection对象添加项创建了一个集合Magento自定义集合中断分页,magento,collections,pagination,Magento,Collections,Pagination,我通过向Varien_Data_collection对象添加项创建了一个集合 $collection = new Varien_Data_Collection(); foreach($array_of_products as $productId){ $collection->addItem(Mage::getModel('catalog/product')->load($productId)); } 但是,当此对象按如下所示传递到Magento pager块时,它会中
$collection = new Varien_Data_Collection();
foreach($array_of_products as $productId){
$collection->addItem(Mage::getModel('catalog/product')->load($productId));
}
但是,当此对象按如下所示传递到Magento pager块时,它会中断自定义页面中的分页
$pager = $this->getLayout()->createBlock('page/html_pager', 'retailerfe.analysis.pager')
->setCollection($collection);
顺便说一句,我从没有遇到过收集的问题
模型集合,如Mage::getModel('module/modelname')->getCollection()。它只是通过将项目添加到Varien_Data_集合对象而创建的集合 我只是怀疑(可能是我错了):
应该是这样的:
$collection->addItem(Mage::getModel('catalog/product')->load($productId)->getData());
如果对你有效,请告诉我。
谢谢
编辑:我终于明白了。 以下是您应该如何做到这一点:
<?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);
}
寻呼机正在调用您的收藏,如果您对其进行跟踪,则该收藏仅由用户使用。这意味着寻呼机可以准确地显示页数,但仅此而已。它实际上是通过将当前页码和大小呈现为SQL的LIMIT
子句来处理它们的
要创建符合页面条件的集合,必须创建该类的子体。有关想法,请查看它是如何实现的loadData
我刚刚重读了你的问题,意识到你可以做到:
$collection = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($array_of_products);
此集合将非常愉快地翻页。提供了解决方案
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;
}
}
谢谢,
Kashif使用以下类扩展Varien数据采集类:
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;
}
}
class Pageable_Varien_Data_Collection扩展了Varien_Data_Collection
{
/**
*加载数据
*
*@param bool$printQuery
*@param bool$logQuery
*
*@return Pageable\u Varien\u数据采集
*/
公共函数加载($printQuery=false,$logQuery=false)
{
如果($this->isLoaded()){
退还$this;
}
$this->_renderLimit();
$this->_setIsLoaded();
退还$this;
}
/**
*@return Pageable\u Varien\u数据采集
*/
受保护函数_renderLimit()
{
如果($this->\u pageSize){
$currentPage=$this->getCurPage();
$pageSize=$this->\u pageSize;
$firstItem=($currentPage-1)*$pageSize+1);
$lastItem=$firstItem+$pageSize;
$iterator=1;
foreach($this->getItems()作为$key=>$item){
$pos=$iterator;
$iterator++;
如果($pos>=$firstItem&$pos removitembyKey($key));
}
}
退还$this;
}
/**
*检索集合所有项目计数
*
*@return int
*/
公共函数getSize()
{
如果(为空($this->\u totalRecords)){
$this->_totalRecords=count($this->getItems());
}
返回intval($this->\u totalRecords);
}
/**
*检索集合项
*
*@return数组
*/
公共函数getItems()
{
返回$this->\u项目;
}
}
请发布错误消息的文本,并告诉我们Magento.v1.4的哪个版本改变了分页的工作方式。没有错误消息。只是分页变得有点奇怪。寻呼机正确计算页码。似乎无法对集合应用限制。所有项目都显示在所有页面上页面。我使用Magento v 1.5.0.1,每当通过向空的Varien_Data_collection对象添加项目创建的集合进行分页时,就会出现这种行为。您能解决此问题吗?需要Varien_对象,而不是数组。同意@clockworkgeek。我还是尝试了。给我一个错误。调用成员函数n getId()在第390行的/var/www/magento/lib/Varien/Data/Collection.php中的非对象上。感谢您的回复。这对我的分页没有帮助。正如@clockworkgeek所说,Varien_Data_Collection本身对分页没有帮助。感谢您的回复。谢谢,clockworkgeek。您的解释是正确的。如果我能从中得到一个好的解决方案,我将在这里发布。我必须创建一个自定义集合并将两个Magento集合合并到其中…实际上,上面的建议很好:它修复了分页。我只是实现了一个集合,扩展了Varien_Data_Collection,并以Varien_Data_Collection_文件系统中描述的类似方式实现了loadData。有人可以发布关于如何在我们的自定义Varien_Data_Collection_文件系统中实现“Varien_Data_Collection_Filesystem”loadData方法的代码?我试图解决这个分页问题几个小时都没有成功:(@Francescosorry现在它花了很多时间,不记得了。只要看看Varien_Data_Collection_文件系统及其loadData()方法。我的建议是查看loadData()
仅为灵感,而不是直接实现它,也不是准确地重新创建它。查看它如何计算$from
,$to
和$isPaginated
,然后在调用$this->addItem()
之前检查它们。秘密是只调用addItem()
用于您知道的当前页面中的项目。如果您对此有更具体的问题,请尝试开始一个新问题。
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;
}
}