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

ぐ巨炮叔叔 提交于 2019-12-08 07:44:25

问题


I'm trying to get only those products which have specific brand and category.

in Product model i used this query

$products = $this->find('all', array(
        'joins' => array(
             array('table' => 'brands_products',
                'alias' => 'BrandsProducts',
                'type' => 'INNER',
                'conditions' => array(
                    'BrandsProducts.brand_id' => $brandId,
                    'BrandsProducts.product_id = Product.id'
                )
            )
        ),
        'group' => 'Product.id'
    ));

By using above query i can get only those products which has specific brand id.

Exactly i don't know how to use multiple conditions to get data from 2 HABTM tables. in my example product HABTM relation with Category and Brands.

$products = $this->find('all', array(
        'joins' => array(
             array('table' => 'brands_products',
                'alias' => 'BrandsProducts',
                'type' => 'INNER',
                'conditions' => array(
                    'BrandsProducts.brand_id' => $brandId,
                    'BrandsProducts.product_id = Product.id'
                )
            ),
             array('table' => 'categories_products',
                'alias' => 'CategoriesProducts',
                'type' => 'INNER',
                'conditions' => array(
                    'CategoriesProducts.category_id' => $categoriesId,
                    'CategoriesProducts.product_id = Product.id'
                )
            )

        ),
        'group' => 'Product.id'
    ));

By using this query i got error in db. SO i'm looking how to write proper query to get only those products where brand and category match.

来源:https://stackoverflow.com/questions/29092064/how-use-multiple-conditions-to-get-data-from-2-habtm-tables-in-cakephp-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!