0

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

  • **https://stackoverflow.com/questions/26799094/how-to-filter-by-conditions-for-associated-models** – ndm May 29 '17 at 12:12
  • The answer is similar to this one https://stackoverflow.com/questions/44220965/cakephp-query-conditional-based-on-contain-field – diego182 May 29 '17 at 17:26

0 Answers0