cakephp 对多个 habtm 进行分页

发布于 2024-08-24 17:50:58 字数 2000 浏览 8 评论 0原文

我有多个像这样的习惯:

// 用户模型
var $hasMany = array('Post');

// 发布模型
var $hasAndBelongsToMany = array('类别', '标签');

// 类别模型
var $hasAndBelongsToMany = array('Post');

// 标记模型
var $hasAndBelongsToMany = array('Post');

我尝试获取所有帖子及其用户和标签(在某个类别内),但不知何故,如果我获取标签,结果是错误的。

$this->paginate = array
(
    'Post' => array
    (
        'limit' => 2,

        'fields' => array(
            'Post.title', 'Post.content', 'Post.slug', 'Post.created',
            'Tag.name',
            'User.username', 'User.created', 'User.post_count', 'User.avatar_file_name'),

        'joins' => array
        (
            array(
                'table' => 'categories_posts',
                'alias' => 'CategoriesPost',
                'type' => 'inner',
                'conditions'=> array('CategoriesPost.post_id = Post.id')
            ),

            // FETCH USER
            array(
                'table' => 'users',
                'alias' => 'User',
                'type' => 'inner',
                'conditions'=> array('Post.user_id = User.id')
            ),

            // FETCH TAGS
            array(
                'table' => 'posts_tags',
                'alias' => 'PostsTag',
                'type' => 'inner',
                'conditions'=> array('PostsTag.post_id = Post.id')
            ),

            array(
                'table' => 'tags',
                'alias' => 'Tag',
                'type' => 'inner',
                'conditions'=> array('Tag.id = PostsTag.tag_id')
            ),

            array(
                'table' => 'categories',
                'alias' => 'Category',
                'type' => 'inner',
                'conditions'=> array('Category.id = CategoriesPost.category_id', 'Category.slug' => $slug)
            )
        )
    )
);

$posts = $this->paginate();

因为我是新手,有人可以给我一个解决方案吗? 非常感谢...

i have multiple habtm like these :

// User model
var $hasMany = array('Post');

// Post model
var $hasAndBelongsToMany = array('Category', 'Tag');

// Category model
var $hasAndBelongsToMany = array('Post');

// Tag model
var $hasAndBelongsToMany = array('Post');

I tried to fetch all post along with its user and tags (within a certain category), somehow if i fetch tags, the result was wrong.

$this->paginate = array
(
    'Post' => array
    (
        'limit' => 2,

        'fields' => array(
            'Post.title', 'Post.content', 'Post.slug', 'Post.created',
            'Tag.name',
            'User.username', 'User.created', 'User.post_count', 'User.avatar_file_name'),

        'joins' => array
        (
            array(
                'table' => 'categories_posts',
                'alias' => 'CategoriesPost',
                'type' => 'inner',
                'conditions'=> array('CategoriesPost.post_id = Post.id')
            ),

            // FETCH USER
            array(
                'table' => 'users',
                'alias' => 'User',
                'type' => 'inner',
                'conditions'=> array('Post.user_id = User.id')
            ),

            // FETCH TAGS
            array(
                'table' => 'posts_tags',
                'alias' => 'PostsTag',
                'type' => 'inner',
                'conditions'=> array('PostsTag.post_id = Post.id')
            ),

            array(
                'table' => 'tags',
                'alias' => 'Tag',
                'type' => 'inner',
                'conditions'=> array('Tag.id = PostsTag.tag_id')
            ),

            array(
                'table' => 'categories',
                'alias' => 'Category',
                'type' => 'inner',
                'conditions'=> array('Category.id = CategoriesPost.category_id', 'Category.slug' => $slug)
            )
        )
    )
);

$posts = $this->paginate();

could anyone gimme a solution since i'm a newbie?
many thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2024-08-31 17:50:58

当尝试使用关联模型对结果集进行分页时,我遇到了类似的问题。我最终不得不手动将模型绑定在一起,运行查询,然后取消绑定它们,以便让 Cake 包含正确的数据。 ( http://book.cakephp.org /view/86/Creating-and-Destroying-Associations-on-the-Fly

您还可以尝试可包含行为,它允许您指定要包含在结果集中的模型。 Containable 是 1.2+ 的核心( http://book.cakephp.org/view/474/Containable ),否则您需要自己获取它。

我不太确定为什么你有如此庞大的查询。我更倾向于做类似以下的事情。

$this->Model->recursive = 2;
$this->Model->paginate();

并且让Cake通过我的协会为我获取所有相关数据。然后我将使用条件数组调整返回( http://api.cakephp.org /class/controller#method-Controllerpaginate )来指定类别。

抱歉,这不是事实上的解决方案,但我自己也是一名 CakePHP 业余爱好者!您可能会发现使用 DebugKit 可以更轻松地查看查询、结果等, http://github.com/cakephp/调试工具包

I've run into a similar problem, when trying to paginate a resultset using an associated model. I ended up having to manually bind my models together, run the query, and then unbind them in order to get Cake to contain the right data together. ( http://book.cakephp.org/view/86/Creating-and-Destroying-Associations-on-the-Fly )

You could also try the containable behaviour which will allow you to specify which models you want to include in your result set. Containable is core in 1.2+ ( http://book.cakephp.org/view/474/Containable ), otherwise you'll need to grab it yourself.

I'm not too sure on why you have such a gargantuan query there though. I would be more inclined to do something similar to the following.

$this->Model->recursive = 2;
$this->Model->paginate();

And let Cake get all the related data for me through my associations. Then I would adjust the return using a conditions array ( http://api.cakephp.org/class/controller#method-Controllerpaginate ) to specify the category.

Sorry it's not a defacto solution, but I'm a CakePHP amateur myself! You might find it easier to view the queries, results etc, using DebugKit, http://github.com/cakephp/debug_kit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文