Thursday, 15 March 2012

mysql - how use multiple conditions to get data from 2 HABTM tables in Cakephp 2.x -


I am trying to find products that have a specific brand and category.

I used this query in the product model

  $ products = $ this- & gt; Search ('all', array ('joins' = & gt; array (array ('table' = & gt; 'brand_products' 'nickname' = & gt; brand products, 'type' = & gt; 'INNER' 'Conditions' = & gt; array (' brands products. Brands_id '=> gt; $ brandId,' brand products.product_id = product.id ')),' group '=> gt; product.id')) ;  

By using the above question, I can only find products that have a specific brand ID

Not at all, I have to get data from 2 HBTM tables How do I use several conditions? Regarding my example product HABTM with category and brand

  $ products = $ this- & gt; Find ('all', array ('joins' => array (array (' table '= & gt; brand_products',' alias' => 'brand products',' type '=> INNER 'Array' ('Brand' products.brand_ID '=>> $ brandId,' brand.products.product_id = 'Product.id')), array ('table' = & gt; 'categories_products' , 'Nickname' = & gt; category products ',' type '= & gt;' INNER ',' conditions '= & gt; array (' Categories Products .category_id '= & gt; $ categoriesId,' Categories Products Product_id = product.a D '))))' group '= & gt;' product.id '));  

Using this query, I found an error in DB, so I see how to write just the right questions to get the products where the brand and category match


No comments:

Post a Comment