2014-10-22 78 views
0

我正在尝试使用CPagination。此处显示的示例http://www.yiiframework.com/doc/api/1.1/CPagination正在使用条件。我正在使用存储过程来选择数据。使用的数据库是postgresql。我正在使用create命令函数调用存储过程。在Yii框架中使用带createCommand函数的CPagination

$command = Yii::app()->db->createCommand("select sp_images_select(:project_id,:milestone_id);"); 
$command->bindParam(":project_id",$this->project_id,PDO::PARAM_STR); 
$command->bindParam(":milestone_id",$this->milestone_id,PDO::PARAM_STR); 
$command->queryAll(); 

如何在此命令的结果集中使用CPagination。我正在使用ajax来显示这里解释的图片库http://www.yiiframework.com/wiki/49/update-content-in-ajax-with-renderpartial/

请帮忙。

在此先感谢。

回答

0

只有当您可以以某种方式将存储过程应用于Limit(最大记录检索) 和Offset(从哪个记录开始)时,才能完成此操作。

让我们通过例子一步步走,并分析所需要的

你的Yii控制器,图像

function actionIndex(){ 
    //Not needed, you apply criteria in your stored procedure with the 
    //arguments of your stored procedure. 
    //$criteria=new CDbCriteria(); 

    // In some way you have to be able to determine how many records will 
    // be returned by the stored procedure when no limits are set 
    // (your challenge). 
    $count= ..; // challenge here 

    // create pagination object 
    $pages=new CPagination($count); 

    // Results per page, configure the maximum number of rows to be 
    // displayed (the limit). 
    $pages->pageSize=10; 

    // Set the page you are on. Either the starting page (0) or the page 
    // when the action is called by the linker (clicking on a button to 
    // go to a certanin page). The linker will send the page using get. 
    // Pages are zero based so you have to decrease the received page by 
    // 1. 
    $pages->currentPage = (isset($_GET['page']) ? $_GET['page']-1 : 0) 

    // You can not use this. This method applies the criteria to a plain 
    // vanilla query (not a stored procedure) and extends that query with 
    // a limit and offset. 
    //$pages->applyLimit($criteria); 
    // Instead, you retrieve the limit and offset from the pagination 
    // object and pass it to your stored procdure. F.i. 
    $command->bindParam(":limit",$pages->limit,PDO::PARAM_INT); 
    $command->bindParam(":offset",$pages->offset,PDO::PARAM_INT); 

    // Get the the images with the stored procedure. 
    $images=$command->queryAll(); 

    // Display the images using a view 
    $this->render('index', array(
    'images' => $images, 
     'pages' => $pages 
    )); 
} 

所以,这是你必须做什么,如果你想使用上述方法:

  • 看看你的存储过程支持LimitOffset
  • 如果是这样,扩展存储pocedure的参数来接收LimitOffset,并将其应用
  • 找到一种方法来确定存储过程会导致当未设置LimitOffset中的记录数。这是必要的,因此分页对象可以基于当前页希望这将让你在赛道上

计算Offset ...

+0

Thanks.I是能够通过使用CSqlDataProvider.Now的做分页问题是我正在使用renderPartial()在另一个视图中使用ajax显示此视图的内容。但是,当我点击分页链接时,它将采取行动。 Ajax无法正常工作。 – user1690835 2014-10-27 14:29:13