Zend_Paginator return as objects

ぐ巨炮叔叔 提交于 2020-01-23 08:31:41

问题


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


回答1:


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);



回答2:


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.




回答3:


This might be a new feature, but if you use Zend_Paginator_Adapter_DbTableSelect, you should get objects (Zend_Db_Table_Row) already.



来源:https://stackoverflow.com/questions/4146158/zend-paginator-return-as-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!