CakePHP 多 HABTM 与分页关联

发布于 2024-10-18 02:04:36 字数 832 浏览 3 评论 0原文

对于我正在构建的电子商务应用程序,我使用 CakePHP。 我创建了数据库,然后使用蛋糕烘焙来烘焙我的应用程序。 我的模型全部正确链接,如下所示:

Product hasMany CategoryProduct,ImagesProduct

Category hasMany CategoryProduct

CategoryProduct ownsTo Product,Category

Image hasMany ImagesProduct

ImagesProduct属于Product,Image

我尝试以各种方式获取依赖于category_id的产品分页视图,但没有成功。 我已经成功获得了我想要的数组 $this->CategoryProducts->findByCategoryId($id),但我无法将 cakePHP 中的内置分页器与结果数组一起使用。

有人可以告诉我如何正确地制定 $options['joins'] 数组以传递给分页函数,以获得相同的结果,但以分页方式?

我想要做的 SQL 是这样的:

SELECT p。 * , i.文件名 FROM 产品 p 左连接( 类别_产品 cp、类别 c、图像_产品 ip、图像 i ) ON ( cp.product_id = p.id AND c.id = cp.category_id 且 c.id =2 AND ip.product_id = p.id AND ip.image_id = i.id )

For an e-commerce app that I'm building I am using CakePHP.
I have created the db and afterwards baked my app with cake bake.
My models are all linked up properly as follows:

Product hasMany CategoryProduct,ImagesProduct

Category hasMany CategoryProduct

CategoryProduct belongsTo Product,Category

Image hasMany ImagesProduct

ImagesProduct belongsTo Product,Image

I have tried in various ways to obtain a paginated view of products dependent of the category_id with no succes.
I have managed to obtain the array I wanted with
$this->CategoryProducts->findByCategoryId($id), but I can't use the built-in paginator from cakePHP with the resultant array.

Could somebody tell me how to properly formulate the $options['joins'] array to pass on to the paginate function in order to get the same result, but in a paginated way?

The SQL for what I want to do would be something like this:

SELECT p . * , i.filename
FROM products p
LEFT JOIN (
category_products cp, categories c, images_products ip, images i
) ON ( cp.product_id = p.id
AND c.id = cp.category_id
AND c.id =2
AND ip.product_id = p.id
AND ip.image_id = i.id )

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

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

发布评论

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

评论(1

神仙妹妹 2024-10-25 02:04:36

这是一个困扰我一段时间的问题。如果您使用与 CakePHP 的 HABTM 关联,​​则不必将任一连接模型(CategoryProduct、ImagesProduct)直接关联到您的模型。如果您的表名称不正确,cake cake 可能无法正确获取 HABTM 关联。连接表应为 categories_productsimages_products,这将构成连接模型 CategoriesProductImagesProduct

不管怎样,下面的内容应该可以让您继续按类别进行过滤:

//in products_controller.php:

//Fake a HasOne association, setting the reset parameter to false since pagination
//requires two queries to complete (one for the count, one for the data)

$this->Product->bindModel(array(
    'hasOne' => array(
        'CategoriesProduct
    )
), false);

//Group by products since the HasOne will return multiple rows for each product

$options = array(
    'group' => 'Product.id',
    'conditions' => array(
        'CategoriesProduct.category_id' => $categories
    )
);

//To paginate the result:

$this->paginate = $options;
$products = $this->paginate();

在此示例中,$categories 可以是单个 category_id 或包含category_ids 的数组(即 array('1', ' 2', '3'))。结果将是类别的“OR”。如果您想按“AND”类型条件进行过滤,请检查 http://edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/

希望这有帮助。

This is a question that perplexed me for quite sometime. You shouldn't have to associate either of your join models (CategoryProduct, ImagesProduct) directly to your models if you're using a HABTM association with CakePHP. cake bake may not have picked it up the HABTM association correctly if you didn't have the table names quite right. The join tables should be categories_products and images_products, which would make th join models CategoriesProduct and ImagesProduct.

Either way though, the following should get you going on filtering by categories:

//in products_controller.php:

//Fake a HasOne association, setting the reset parameter to false since pagination
//requires two queries to complete (one for the count, one for the data)

$this->Product->bindModel(array(
    'hasOne' => array(
        'CategoriesProduct
    )
), false);

//Group by products since the HasOne will return multiple rows for each product

$options = array(
    'group' => 'Product.id',
    'conditions' => array(
        'CategoriesProduct.category_id' => $categories
    )
);

//To paginate the result:

$this->paginate = $options;
$products = $this->paginate();

In this example $categories can be either a single category_id or an array containing category_ids (i.e. array('1', '2', '3')). The result will be an 'OR' of categories. If you're looking to filter by an 'AND' type condition, check http://edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/.

Hope this helps.

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