I'm developing a simple webshop where I have a table Sizes that has a many to many relation with Products. I want to find all products by a certain size: /products-by-size/XL where my controller and action is:
public function productsBySize($size){
$slug = $this->request->session()->read('Category.slug');
$gender = $this->request->session()->read('Category.gender');
$this->paginate = [
'contain' => ['Categories', 'Taxes', 'Manufactures', 'Promotions', 'ProductImages', 'Sizes'],
'maxLimit' => 9,
'order' => ['price'=>'ASC']
];
$sub = $this->buildTree($this->Categories->find()->toArray());
$categories = $this->treeToHtml($sub);
$products = $this->paginate($this->Products->find()->where([
'Categories.slug'=>$slug,
'gender'=>$gender,
**'Sizes.name'=>$size**
]));
if($slug == 'promotion'){
$products = $this->paginate($this->Products->find()->where([
'promotion_id >'=>0,
'gender'=>$gender,
'Sizes.name'=>$size
]));
}
if($slug == 'all'){
$products = $this->paginate($this->Products->find('all')->where([
'gender'=>$gender,
'Sizes.name'=>$size
]));
}
$sizes = $this->Sizes->find('all');
$this->set(compact('products', 'slug', 'categories', 'sizes'));
$this->render('products', 'Webshop.default', ['products', 'categories', 'sizes']);
}
My join table is products_sizes and I'm using the standard conventions. 'Sizes.name'=>$size won't work.
Any help? Thx