CakePHP multi-HABTM associations with pagination -


for e-commerce app i'm building using cakephp. have created db , afterwards baked app cake bake. models linked 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 obtain paginated view of products dependent of category_id no succes. have managed obtain array wanted $this->categoryproducts->findbycategoryid($id), can't use built-in paginator cakephp resultant array.

could tell me how formulate $options['joins'] array pass on paginate function in order same result, in paginated way?

the sql want this:

select p . * , i.filename products p left join ( category_products cp, categories c, images_products ip, images ) on ( cp.product_id = p.id , c.id = cp.category_id , c.id =2 , ip.product_id = p.id , ip.image_id = i.id )

this question perplexed me quite sometime. shouldn't have associate either of join models (categoryproduct, imagesproduct) directly models if you're using habtm association cakephp. cake bake may not have picked habtm association correctly if didn't have table names quite right. join tables should categories_products , images_products, make th join models categoriesproduct , imagesproduct.

either way though, following should going on filtering categories:

//in products_controller.php:  //fake hasone association, setting reset parameter false since pagination //requires 2 queries complete (one count, 1 data)  $this->product->bindmodel(array(     'hasone' => array(         'categoriesproduct     ) ), false);  //group products since hasone return multiple rows each product  $options = array(     'group' => 'product.id',     'conditions' => array(         'categoriesproduct.category_id' => $categories     ) );  //to paginate result:  $this->paginate = $options; $products = $this->paginate(); 

in example $categories can either single category_id or array containing category_ids (i.e. array('1', '2', '3')). result 'or' of categories. if you're looking filter 'and' type condition, check http://edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/.

hope helps.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -