0

每次我从寻呼机请求页面时,是否可以使用 zend 分页而不调用查询?

当我从寻呼机中点击页码时,完成了对下面 zzAction 的请求,并再次获取查询。我的查询很大,我不想重新获取查询。我是否遗漏了代码中的某些内容。

代码:

控制器:

public function getOnePageOfEntries($array, $page=1) {
    $paginator = Zend_Paginator::factory($array); 
    $paginator->setItemCountPerPage(6);
    $paginator->setCurrentPageNumber($page);
    return $paginator;
}
public function zzAction() {
   ...

    $tt= $this->yyObject->xx(....);

    $paginator = $this -> getOnePageOfEntries($tt, $page);
    $this->view->paginator = $paginator;

}

模型:

public function xx(...){

    try{
        ...

        $stmt = $this->prepare("CALL sp_yy(...)");
        ....

                    $stmt->execute();

        $result = $stmt->fetchAll();


        if (is_null($result)) {
            return null;
        }

        return $result;

    }catch (ErrorsException $obj){
                echo $obj;exit;
    }//end try
}

看法:

<?php
$config = Zend_Registry::get('appsConfig');
?>
<?php if (count($this->paginator)){ ?>

<?php foreach($this->paginator as $cc){ ?>
<?php echo $cc['id'] . '/';?>
<?php } ?>
<?php } ?>
<?php echo $this->paginationControl($this->paginator,
'Sliding','ff/my_pagination_control.phtml'); ?>
4

2 回答 2

0

使用 Zend_Paginator_Adapater_DbSelect();

例子:

    $adapter     = new Zend_Paginator_Adapter_DbSelect($data); //$data is database query
    $pagination  = new Zend_Paginator($adapter);

在这里查看更多:

Zend_Paginator_Adapter_DbSelect

于 2013-05-30T05:03:50.140 回答
0

您当前的代码人为地限制了数组适配器的实用性,并强制您对每个页面执行整个查询。您需要在使用此分页器的控制器操作中完成的是仅在数据不存在时才执行查询。也许类似于:

//consider this to psuedocode as it has not been tested a represents an idea
public function zzAction()
{ 
    //get page number
    $page = $this->getRequest()->getParam('page');
    //set session namespace, probably better to do this in init() method or bootstrap
    $session = new Zend_Session_Namespace('paged')
    //test for presence of persisted array
    if (!isset($session->paginator)) {
        //perform query
        $arrayToPage = $this->yyObject->xx(....);
        //persist result array
        $session->paginator = $arrayToPage;
    } else {
        //retrieve persisted array
        $arrayToPage = $session->paginator;
    }

    $paginator = $this -> getOnePageOfEntries($arrayToPage, $page);
    $this->view->paginator = $paginator;

}

使用 DbTableSelect 或 DbSelect 分页器适配器通常效率更高,因为它只查询需要填充特定页面的数据。当您的用户想要从第 1 页转到第 7 页时,这非常有用...

使用分页器时的另一个考虑因素是自定义实体模型。这在 ZF 中相当容易处理:

<?php
class Record_Model_Paginator_Record extends Zend_Paginator_Adapter_DbTableSelect
{
    //override getItems to customize the adapter to use a specific mapper to create entities
    public function getItems($offset, $itemCountPerPage)
    {
        $rows = parent::getItems($offset, $itemCountPerPage);

        $record = array();
        foreach ($rows as $row) {
            //initiate mapper
            $recordEntity = new Application_Model_Mapper_Record();
            //create entity models
            $record[] = $recordEntity->createEntity($row);
        }
        //returns an array of objects, similar to a Zend_Db_Rowset object
        return $record;
    }
}

我希望这有帮助。

于 2013-05-30T08:12:45.923 回答