douluoyou9876 2015-06-24 05:39
浏览 37

我们如何在主题Controller中使用paginate执行以下语句?

How can we do the following statement in topics Controller with paginate?

$posts=$this->Topic->Post->find('all',array('conditions'=>array('Post.t‌​opic_id'=>$id)));

Here my intention is that join the Topics and Posts tables, retrive the no of posts where topic id is matching in the posts table using paginate() in cakePHP.

For example if topic id 1 have n no of posts. if change the topic id, that belongs posts will display using paginate().

  • 写回答

1条回答 默认 最新

  • dongyong5255 2015-06-24 06:28
    关注

    You need to use the paginator component rather than a find:-

    $data = $this->Paginator->paginate(
        'Post',
        array('Post.t‌​opic_id' => $id)
    );
    

    The second parameter of paginate filters the result (i.e. these are the query conditions).

    You can also pass the same sort of parameters to paginate as you would with a normal find using $this->paginate. For example:-

    $this->paginate = array(
        'conditions' => array('Post.t‌​opic_id' => $id),
        'order' => 'Post.created DESC'
    );
    $data = $this->Paginator->paginate('Post');
    

    In your case the first example should be sufficient.

    Make sure you load the Paginator component in your controller:-

    class PostsController extends AppController {    
        public $components = array('Paginator');    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害