cakePHP 使用左连接分页会导致页数不正确

发布于 2024-09-27 16:17:55 字数 1356 浏览 0 评论 0原文

我在 cakePHP 分页方面遇到问题,特别是当我进行左连接时。返回 6 行,分页限制为 3,它应该计算 2 页,但它计算 3 页,最后一页为空白。这是我传递给分页的选项数组:

Array
(
    [fields] => Array
        (
            [0] => DISTINCT Video.id, Video.thumbnail_img, Video.title, 
                   Video.description, Video.tags, Video.views, Video.date,
                   ((
                       SELECT COUNT(Rating.id) 
                       FROM ratings AS Rating 
                       WHERE Rating.rating = 1 AND Rating.video_id = Video.id
                    ) - 
                    (
                       SELECT COUNT(Rating.id) 
                       FROM ratings AS Rating 
                       WHERE Rating.rating = 0 AND Rating.video_id = Video.id
                    )
                   ) AS avgRating
        )

    [joins] => Array
        (
            [0] => Array
                (
                    [table] => ratings
                    [alias] => Rating
                    [type] => LEFT
                    [conditions] => Array
                        (
                            [0] => Rating.video_id = Video.id
                        )

                )

        )

    [order] => Array
        (
            [avgRating] => DESC
        )

)

有什么想法为什么会发生这种情况吗?我不确定是否有其他方法可以在不使用连接的情况下获得我想要的结果,但我敢打赌这就是导致问题的原因。

任何建议将不胜感激。谢谢!

I'm having an issue with cakePHP pagination, specifically when I do a left join. There are 6 rows returned, and with a pagination limit of 3, it should calculate 2 pages, but it calculates 3 pages, with the last page being blank. Here is the options array I'm passing to paginate:

Array
(
    [fields] => Array
        (
            [0] => DISTINCT Video.id, Video.thumbnail_img, Video.title, 
                   Video.description, Video.tags, Video.views, Video.date,
                   ((
                       SELECT COUNT(Rating.id) 
                       FROM ratings AS Rating 
                       WHERE Rating.rating = 1 AND Rating.video_id = Video.id
                    ) - 
                    (
                       SELECT COUNT(Rating.id) 
                       FROM ratings AS Rating 
                       WHERE Rating.rating = 0 AND Rating.video_id = Video.id
                    )
                   ) AS avgRating
        )

    [joins] => Array
        (
            [0] => Array
                (
                    [table] => ratings
                    [alias] => Rating
                    [type] => LEFT
                    [conditions] => Array
                        (
                            [0] => Rating.video_id = Video.id
                        )

                )

        )

    [order] => Array
        (
            [avgRating] => DESC
        )

)

Any ideas why this is happening? I'm not sure of any other way to get the results I want without using the join, but I'm betting that's what's causing the problem.

Any suggestions would be appreciated. Thanks!

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

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

发布评论

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

评论(1

带上头具痛哭 2024-10-04 16:17:55

愚蠢的问题 - 在这种情况下甚至不需要连接。无需连接即可获得相同的结果,因为子查询可以自行评估。对于本示例来说,这是可以的,但可能还有其他示例,其中需要连接并且计数错误确实存在。不管怎样,我的问题已经解决了!

Foolish problem - in this case the join isn't even necessary. The same results are achieved without the join, since the sub-queries can be evaluated on their own. For this example, it's ok, but there may be further examples where a join is necessary and the count error does persist. Either way, my problem is solved!

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