cakephp 1.3 paginating habtm related data
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 fie开发者_C百科lds.
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!
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
)
)
);
}
}
You should read this article http://blog.sohaibmuneer.com/using-mysql-inner-join-in-cakephp-pagination and also you can use join in your find query http://book.cakephp.org/1.3/view/1047/Joining-tables
精彩评论