开发者

CakePHP multi-HABTM associations with pagination

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 CategoryProdu开发者_如何学Cct,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 )


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜