简体   繁体   中英

Zend_Paginator return as objects

Zend_Paginator returns results as a standard array but I need to make my results come back as an instance of a class, how do I do this?

For example, I want all news articles so would need my items to come back as an instance of News_Model_Article

You can also create custom zend paginator adapter like:

class Application_Paginator_Adapter extends Zend_Paginator_Adapter_DbSelect
{

  public function getItems($offset, $itemCountPerPage)
  {
    $this->_select->limit($itemCountPerPage, $offset);
    $rowset = $this->_select->getTable()->fetchAll($this->_select);

    $articleModels = array();
    foreach($rowset as $row) {
      $model = new News_Model_Article();
      $model->setTitle($row->article_title);
      ...........
      $articleModels[] = $model;
    }

    return $articleModels;
  }

}

Use it as below:

$adapter = new Application_Paginator_Adapter();
$paginator = new Zend_Paginator($adapter);

Use the Db_Table Paginator, you can initiate it like this:

$table = new Your_Zend_DbTable(); // Asuming this has configured the $rowClass property
$paginator = Zend_Paginator::factory($table->select());
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber(1);

Then you can loop the $paginator object and read the object properties.

这可能是一个新功能,但是如果你使用Zend_Paginator_Adapter_DbTableSelect ,你应该已经获得了对象( Zend_Db_Table_Row )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM