4

我想为自定义数组分页。我的控制器和视图代码如下。

控制器代码:

public function admin_detail(){
        $totalByDate = $this->Pbscodemetric->find('all',array('fields'=>array('created')));
        $createdDate = Set::extract('/Pbscodemetric/created', $totalByDate);
        $uniqueCreatedDate = array_unique($createdDate);
        $finalArray = array();
        foreach($uniqueCreatedDate as $created){
            $downloadsArr = $this->Pbscodemetric->find('all',array('fields'=>array('downloads_iphone','downloads_ipad','downloads_android'),'conditions'=>array('created'=>$created)));
            $download_iphone = array_sum(Set::extract('/Pbscodemetric/downloads_iphone',$downloadsArr));
            $download_ipad = array_sum(Set::extract('/Pbscodemetric/downloads_ipad',$downloadsArr));
            $downloads_android = array_sum(Set::extract('/Pbscodemetric/downloads_android',$downloadsArr));
            $finalArray[$created] = array(
                'downloads_iphone' => $download_iphone,
                'downloads_ipad' => $download_ipad,
                'downloads_android' => $downloads_android
            );
        }
        $this->set('finalArray',$finalArray);
    }

查看代码:

<div class="pbscodemetrics index">
    <h2><?php echo __('Pbscodemetrics List Detail'); ?></h2>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>Date</th>
            <th>iPhone</th>
            <th>iPad</th>
            <th>Android</th>    
        </tr>
        <?php 
        foreach($finalArray as $key => $final){?>
            <tr> 
                <td><?php echo $key;?></td>
                <td><?php echo $final['downloads_iphone'];?></td>
                <td><?php echo $final['downloads_ipad'];?></td>
                <td><?php echo $final['downloads_android'];?></td>
            </tr>
            <?php }?>
    </table>
</div>
<div class="actions">
    <?php echo $this->element('nav');?>
</div>

现在我想设置分页。我知道 CakePHP 默认分页需要模型名称。但是我怎样才能为上面的代码做到这一点?

任何人都可以帮助我吗..

4

1 回答 1

2

您可以实现自定义分页,请参阅Cookbook 中的自定义查询分页。但是,看起来您在那里所做的事情可以使用分组和 SQLSUM函数来完成,这样您就可以简单地使用内置的分页。例子:

$alias = $this->Pbscodemetric->alias;

$this->Pbscodemetric->virtualFields = array
(
    'downloads_iphone_sum' => sprintf('SUM(`%s.downloads_iphone`)', $alias),
    'downloads_ipad_sum' => sprintf('SUM(`%s.downloads_ipad`)', $alias),
    'downloads_android_sum' => sprintf('SUM(`%s.downloads_android`)', $alias)
);

$this->paginate = array
(
    'fields' => array
    (
        'downloads_iphone_sum',
        'downloads_ipad_sum',
        'downloads_android_sum',
        'created'
    ),
    'group' => 'created',
    'order' => array
    (
        'created' => 'desc'
    ),
    'limit' => 10
);

$data = $this->paginate($alias);

这将为您提供Paginator 助手兼容的结果,其中downloads_iphonesummeddownloads_ipaddownloads_android列按列分组created。它使用虚拟字段来检索默认 CakePHP 样式的结果,即它们可以像这样访问:

    <?php foreach($results as $result): ?>
        <tr> 
            <td><?php echo $result['Pbscodemetric']['created'];?></td>
            <td><?php echo $result['Pbscodemetric']['downloads_iphone_sum'];?></td>
            <td><?php echo $result['Pbscodemetric']['downloads_ipad_sum'];?></td>
            <td><?php echo $result['Pbscodemetric']['downloads_android_sum'];?></td>
        </tr>
    <?php endforeach; ?>
于 2012-11-05T19:33:44.293 回答