【问题标题】:Paginating associated models with CakePHP 2.x使用 CakePHP 2.x 对关联模型进行分页
【发布时间】:2014-01-11 06:02:51
【问题描述】:

我正在使用 CakePHP 2 开发图库软件,但在尝试对内容进行分页时遇到了问题。

我在网站上有两个主要模型,它们是 AlbumImage。它们是由一个有很多关系的,一个相册可以有很多张图片,每张图片都属于一个相册。我可以在我的 AlbumsController.php 文件中使用以下内容来毫无问题地对专辑列表进行分页:

public $paginate = array(
    'limit' => 21
);

然后,在我要分页的方法中(例如index):

public function index() {
    $this->set('albums', $this->paginate());
}

我在为相册视图分页时遇到问题。对于相册视图,系统使用关系来获取属于该相册的所有图像,使用以下函数:

public function view($id = null) {

        if (!$id) {
            throw new NotFoundException(__('Album not found.'));
        }

        $album = $this->Album->findById($id);

        if (!$album) {
            throw new NotFoundException(__('Album not found.'));
        }

    $this->set('album', $album);
}

但是,我不知道如何让系统根据图像对相册视图进行分页。我试过使用:

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

无济于事(系统似乎忽略了它),同时还尝试重载paginate() 函数,但只得到错误。那么,任何人都可以为我指出正确的方向吗?这是我在这里的一个非常简单的关系,我相信答案也很可能是一个非常简单的关系,但我似乎找不到它,甚至在 Stack Overflow 中,我发现的大多数相关线程都问得更复杂在关系中进行搜索的系统。我不需要这些,这是一个非常简单的分页案例。

【问题讨论】:

    标签: php cakephp pagination gallery


    【解决方案1】:

    可能对你有帮助:

    class AlbumsController extends AppController {
    
        var $name = 'Albums';
    
        var $paginate = array(
        'limit' => 95,
        'order' => array(
        'Album.name' => 'asc')
        );
    
        function index() {
                $this->Album->recursive = 0;
                $this->set('albums', $this->paginate());
                $this->set("title_for_layout","CakeGal");
        }
    
        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid album', true));
                        $this->redirect(array('action' => 'index'));
                }
                $this->set('album', $this->Album->read(null, $id));
                $this->set("title_for_layout", $this->Album->data['Album']['name']);
        }
    

    }

    【讨论】:

    • 你能告诉我视图函数被要求在哪里分页吗?我似乎看不到它:\
    【解决方案2】:

    我终于搞定了。下面是函数最终的样子:

    public function view($id = null){
    
        if (!$id) {
            throw new NotFoundException(__('Album not found.'));
        }
    
        $album = $this->Album->findById($id);
    
        if (!$album) {
            throw new NotFoundException(__('Album not found.'));
        }
    
        $this->paginate = array(
            'conditions'=>array(
                'Image.album_id'=>$id),
            'limit'     =>'21'
        );
    
        $album = $this->paginate('Image');
    
        $this->set('album', $album);
        $this -> set('id', $id);
    }
    

    所以我所做的是为分页数组添加一组新的约束,然后强制它根据专辑 ID 对图像进行分页。这也产生了强迫我重写我的专辑view.ctp 文件的附带效果,因为它完全改变了模板接收数据的方式。在这种情况下,我不得不停止使用foreach($album['Image'] as $image) 来浏览图像,而是使用while($image_no < 21) 循环来访问$album[number]['Image'] 的每个实例。

    我确信必须有更好/更优雅的方法来实现我所需要的,但由于我找不到任何其他方法,我将这里留作记录,以防其他人遇到同样的问题.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2010-11-30
      • 2017-08-14
      • 2013-11-21
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多