cakephp 1.3 对habtm相关数据进行分页

发布于 2024-10-11 01:39:46 字数 373 浏览 4 评论 0原文

我已经四处搜索并尝试了多种不同的解决方案,但我似乎无法让它发挥作用。 基本上我有产品和类别。 类别具有并属于许多产品,并且产品为 HABTM 类别。它们显然在连接表 products_categories 中与 id、product_id、category_id 字段相关。

经过简单的完整烘焙后,模型视图和控制器被烘焙 - 添加了一些类别和产品并设置了它们的关系,我可以转到特定的类别视图页面,预烘焙视图将显示与此类别相关的产品。

然而,有相当多的产品(而且还会更多),我正在努力弄清楚如何对所有这些相关数据进行分页……因为仅仅简单地列出一个类别中的所有产品是不可行的(可能有100 秒或更多) 有人可以帮忙吗?似乎关于这个问题还有很多未解答的主题以及各种不同的解决方案,我都无法开始工作!请帮忙!

I have searched high and low and tried multiple different solutions to this and I cannot seem to get it to work.
Basically I have products and categories.
Categories has and belongs to many products, and products HABTM categories. They are obviously related in a join table products_categories with id,product_id,category_id fields.

After a simple full bake- models views and controllers are baked - some categories and products are added and their relationships are set I can go to a specific category view page and the pre baked view will show products that are related to this category.

However, there is quite a few products (and will be more) and I am struggling to work out how to paginate all of this related data..as it is unfeasible to just simply list all of the products in a category (there could be 100s or more)
Can anybody help? It seems there are quite a few unanswered topics on this and various different solutions, none of which I can get to work! Please help!

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

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

发布评论

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

评论(2

合久必婚 2024-10-18 01:39:46

CakePHP 控制器具有内置分页。该文档非常详尽,因此我建议您首先阅读该文档。之后,我很乐意回答您对此主题的任何问题。

在不知道所涉及的具体代码的情况下,以下是如何对模型数据进行分页的简单示例:

class CategoryController extends AppController {
    var $paginate = array(
        'Product' => array(
            'joins' => array(
                array(
                    'table' => 'categories_products', // or products_categories
                    'alias' => 'CatFilter',
                    'type' => 'INNER',
                    'conditions' => array(
                        'CatFilter.product_id = Product.id'
                    )
                )
            ),
            'limit' => 10
         )
    );

    function view ($category_id) {
        $this->set(
            'products', $this->paginate(
                $this->Category->Product, array(
                    'CatFilter.category_id' => $category_id
                )
             )
        );
    }
}

CakePHP controllers have built in pagination. The documentation is thorough, so I would recommend reading that first. Afterwards, I'd be happy to answer any questions you have on the subject.

Without knowing the specific code involved, here is a simple example of how to paginate your model data:

class CategoryController extends AppController {
    var $paginate = array(
        'Product' => array(
            'joins' => array(
                array(
                    'table' => 'categories_products', // or products_categories
                    'alias' => 'CatFilter',
                    'type' => 'INNER',
                    'conditions' => array(
                        'CatFilter.product_id = Product.id'
                    )
                )
            ),
            'limit' => 10
         )
    );

    function view ($category_id) {
        $this->set(
            'products', $this->paginate(
                $this->Category->Product, array(
                    'CatFilter.category_id' => $category_id
                )
             )
        );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文