CakePHP - 分页数组 [英] CakePHP - Paginating an Array

查看:127
本文介绍了CakePHP - 分页数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cake使用一个简单的$ this-> paginate()处理一个模型的分页,但是如果我想分配一个值数组,应该怎么使用呢?

Cake handles pagination of a model with a simple $this->paginate(), but what should I use if I want to paginate a array of values?

方案如下:

$this->set('sitepages', $this->paginate());

我的索引()中的代码返回一个类似

This code in my index() returns an array like

Array
(
    [0] => Array
        (
            [Sitepage] => Array
                (
                    [id] => 13
                    [name] => Home
                    [urlslug] => home
                    [parent_id] => 1
                    [page_title] => Welcome to KIAMS, Pune
                    [order] => 1
                )   
        )
    [1] => Array
        (
            [Sitepage] => Array
                (
                    [id] => 26
                    [name] => About Us
                    [urlslug] => aboutus
                    [parent_id] => 1
                    [page_title] => 
                    [order] => 2
                )
        )
    [2] => Array
        (
            [Sitepage] => Array
                (
                    [id] => 27
                    [name] => Overview of KIAMS
                    [urlslug] => aboutus/overview
                    [parent_id] => 26
                    [page_title] => 
                    [order] => 2
                )
        )

我使用$ this-> Sitepage-> find('all')检索相同的数据,然后根据需要执行一些操作,形成一个非常类似于上面一个,但是顺序改变了。我想分页这个新的数组,并将其传递给视图。我尝试了

I retrieved the same data using $this->Sitepage->find('all') and then performed some manipulations as required and form a array which is very similar to the above one, but the ordering gets changed. I want to paginate this new array and pass it to the view. I tried

$this->set('sitepages',$this->paginate($newarray))

但是数据没有分页。可能有人请帮助在CakePHP中分页$ newarray?

But the data is not getting paginated. Can some one please help with paginating the $newarray in CakePHP?

推荐答案

要在CakePHP中分页, paginate()调用。

To paginate in CakePHP you need to pass select conditions to the paginate() call.

其他数据操作应该在afterFind()中,在你的模型文件中。

Other data manipulation should be done in afterFind(), in your model file.

如果您不需要在每次检索时都进行这些更改,您可以考虑创建一个指向与当前表格相同的表格的新模型文件,并向该新文件添加一个afterFind()方法。

If you don't need these changes to be done in every single retrieval, you might as well consider creating a new model file pointing to the very same table as the current one, and adding an afterFind() method to that new file.

这篇关于CakePHP - 分页数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆