CakePHP多HABTM协会与分页

编程入门 行业动态 更新时间:2024-10-10 08:26:12
本文介绍了CakePHP多HABTM协会与分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于我正在建设的电子商务应用程序,我使用CakePHP。 我创建了db,然后用 cake bake 烘焙我的应用程序。 我的模型都正确链接如下:

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:

产品hasMany CategoryProduct,ImagesProduct

类别hasMany CategoryProduct

belongsTo产品,类别

图片hasMany ImagesProduct

ImagesProduct belongsTo产品,图像

我尝试过各种方式获得分页取决于category_id的产品视图,没有成功。 我已经设法通过 $ this-> CategoryProducts-> findByCategoryId($ id)获得了我想要的数组,但我可以' t使用内置的paginator from cakePHP与结果数组。

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.

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

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)

推荐答案

这是一个让我困惑了一段时间的问题。如果您使用与CakePHP的HABTM关联,则不必将任何连接模型(CategoryProduct,ImagesProduct)直接关联到您的模型。 cake bake 可能没有正确选择HABTM关联,如果你没有正确的表名。连接表应该是 categories_products 和 images_products ,这将使连接模型 CategoriesProduct 和 ImagesProduct 。

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();

在此示例中,$ categories可以是单个 category_id 或包含category_ids的数组(即 array('1','2','3'))。结果将是类别的或。如果您希望通过AND类型条件过滤,请选中 edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/ 。

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 edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/.

希望这有助于。

更多推荐

CakePHP多HABTM协会与分页

本文发布于:2023-08-03 14:50:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287776.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   协会   CakePHP   HABTM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!