CakePHP 多 HABTM 与分页关联
对于我正在构建的电子商务应用程序,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个困扰我一段时间的问题。如果您使用与 CakePHP 的 HABTM 关联,则不必将任一连接模型(CategoryProduct、ImagesProduct)直接关联到您的模型。如果您的表名称不正确,
cake cake
可能无法正确获取 HABTM 关联。连接表应为categories_products
和images_products
,这将构成连接模型CategoriesProduct
和ImagesProduct
。不管怎样,下面的内容应该可以让您继续按类别进行过滤:
在此示例中,$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 becategories_products
andimages_products
, which would make th join modelsCategoriesProduct
andImagesProduct
.Either way though, the following should get you going on filtering by categories:
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.