2012-07-16 40 views

回答

4

setPage()方法僅適用於在Magento EAV基於集合,因爲它是在Mage_Eav_Model_Entity_Collection_Abstract類中定義...

public function setPage($pageNum, $pageSize) 
{ 
    $this->setCurPage($pageNum) 
     ->setPageSize($pageSize); 
    return $this; 
} 

正如你可以看到,它的一個很好的速記工具,可用於EAV基於集合。爲了您的非EAV根據收集你可以在你的集合類創建自己的這個版本或使用了更詳細的語法用於初始化集合時,設置在客戶端代碼的網頁數量和規模:

$collection->setCurPage($pageNum) 
      ->setPageSize($pageSize) 
; 
1
public function updateIndex() 
{ 
    $productsCollection = Mage::getModel('catalog/product')->getCollection() 
     ->addAttributeToSelect(array('name', 'image', 'url_key', 'price', 'visibility')); 

    $productsCollection->setPageSize(100); 

    $pages = $productsCollection->getLastPageNumber(); 
    $currentPage = 1; 

    do { 
     $productsCollection->setCurPage($currentPage); 
     $productsCollection->load(); 

     foreach ($productsCollection as $_product) { 

      $insertData = array(
       'entity_id' => $_product->getId(), 
       'title' => $_product->getName(), 
       'image' => $_product->getImage(), 
       'url' => $_product->getUrlKey(), 
       'price' => $_product->getFinalPrice(), 
      ); 

      $this->_getWriteAdapter()->insertOnDuplicate(
       $this->getTable('atwix_sonar/suggestions'), 
       $insertData, 
       array('title', 'image', 'url', 'price') 
      ); 
     } 

     $currentPage++; 
     //clear collection and free memory 
     $productsCollection->clear(); 
    } while ($currentPage <= $pages); 
} 

看到這個完整的例子,它說明了如何使用magento collection pager功能。 來源:http://www.atwix.com/magento/working-with-large-collections/

相關問題