CakePHP分页与HABTM模型 [英] CakePHP pagination with HABTM models

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

问题描述

我在使用HABTM关系创建分页时遇到一些问题。首先,表和关系:

I'm having some problems with creating pagination with a HABTM relationship. First, the tables and relationships:

requests (id, to_location_id, from_location_id)
locations (id, name)
items_locations (id, item_id, location_id)
items (id, name)

因此,请求具有来自的请求的位置和请求正在进行 的位置。对于此问题,我只关心到位置。

So, a Request has a Location the request is coming from and a Location the Request is going to. For this question, I'm only concerned about the "to" location.

Request --belongsTo--> Location* --hasAndBelongsToMany--> Item

(* as "ToLocation")

在我的RequestController想要对请求的ToLocation中的所有项目进行分页。

In my RequestController, I want to paginate all the Items in a Request's ToLocation.

// RequestsController
var $paginate = array(
    'Item' => array(
        'limit' => 5,
        'contain' => array(
            "Location"
        )
    )
);

// RequestController::add()
$locationId = 21;
$items = $this->paginate('Item', array(
    "Location.id" => $locationId
));

这是失败,因为它生成这个SQL:

And this is failing, because it is generating this SQL:

SELECT COUNT(*) AS count FROM items Item   WHERE Location.id = 21

我不知道如何让它实际使用 $ paginate ...

I can't figure out how to make it actually use the "contain" argument of $paginate...

任何想法?

推荐答案

p>

after 3 days searching, I found the way

var $paginate = array('Post'=>array('group'=>'Post.id'));

建议您新增群组,因为有时候我们会取得不同类别的重复文章

It's recomended to add group, because sometimes we will get duplicte posts in different categories

$this->Post->bindModel(array('hasOne'=>array('CategoriesPost')), false);
$out = $this->paginate('Post', array('CategoriesPost.category_id'=>array(1,4,7,6)));

添加 false 可对所有查询使用绑定模式,

Add false to use bind model to all queries, not only to the following

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

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