CakePHP发现所有条件都被忽略

时间:2013-10-10 19:13:57

标签: cakephp

我正在使用CakePHP 1.3,但在将SQL查询转换为CakePHP时遇到了一些麻烦。查询背后的概念是检索与指定帐户关联并属于特定产品类别或指定类别的直接子项的所有产品。

这里涉及三个表,product,product_categories和category_types。 product_categories是一个将产品连接到category_types的链接表。 category_types表是可以连接到自身的树结构。

如果类别的id为1且帐户的id为1,则查询的SQL最终应该看起来像这样:

SELECT Product.id, Product.name, Product.account_id
FROM products AS Product
INNER JOIN product_categories AS pc ON (Product.id = pc.product_id) 
INNER JOIN category_types AS ct ON (ct.id = pc.category_type_id) 
WHERE ( 
   ((Product.account_id IS NULL) OR (Product.account_id = '1'))
   AND ((ct.id = '1') OR (ct.parent_id = '1')) 
)

以下是我用来尝试执行此操作的代码,该代码位于我的模型中的函数中:

$this->find('all', array(
    'joins' => array(
        array(
            'table' => 'product_categories',
            'alias' => 'pc',
            'type' => 'INNER',
            'conditions' => array(
                'Product.id = pc.product_id'
            )
        ),
        // get category type
        array(
            'table' => 'category_types',
            'alias' => 'ct',
            'type' => 'INNER',
            'conditions' => array(
                'pc.category_type_id = ct.id'
            )
        )
    )            
    , 'conditions'=> array(
        'OR' => array(
            'Product.account_id IS NULL'
            , 'Product.account_id' => $account_id
        )
        , 'OR' => array(
            'ct.id' => $categoryTypeId
            , 'ct.parent_id' => $categoryTypeId
        )
    )
));

问题是查询忽略了导致SQL的account_id OR条件,如下所示:

SELECT Product.id, Product.name, Product.account_id
FROM products AS Product
INNER JOIN product_categories AS pc ON (Product.id = pc.product_id) 
INNER JOIN category_types AS ct ON (ct.id = pc.category_type_id) 
WHERE ((ct.id = '1') OR (ct.parent_id = '1'))

如何更改我的PHP代码以获得所需的结果?

2 个答案:

答案 0 :(得分:0)

感谢user2076809让我指向正确的方向。解决方案是将两个OR条件包装在各自的数组中。

$this->find('all', array(
    'joins' => array(
        array(
            'table' => 'product_categories',
            'alias' => 'pc',
            'type' => 'INNER',
            'conditions' => array(
                'Product.id = pc.product_id'
            )
        ),
        // get category type
        array(
            'table' => 'category_types',
            'alias' => 'ct',
            'type' => 'INNER',
            'conditions' => array(
                'pc.category_type_id = ct.id'
            )
        )
    ),            
    'conditions'=> array(
        array(
            'OR' => array(
                'Product.account_id IS NULL'
                , 'Product.account_id' => $account_id
            )
        ),
        array(
            'OR' => array(
                'ct.id' => $categoryTypeId
                , 'ct.parent_id' => $categoryTypeId
            )
        )
    )
));

答案 1 :(得分:0)

我认为,你应该将这两个数组只包含在一个OR条件中,如: -

$this->find('all', array(
    'joins' => array(
        array(
            'table' => 'product_categories',
            'alias' => 'pc',
            'type' => 'INNER',
            'conditions' => array(
                'Product.id = pc.product_id'
            )
        ),
        // get category type
        array(
            'table' => 'category_types',
            'alias' => 'ct',
            'type' => 'INNER',
            'conditions' => array(
                'pc.category_type_id = ct.id'
            )
        )
    ),            
    'conditions'=> array(
        'OR' => array(
            array(
                'Product.account_id IS NULL'
                , 'Product.account_id' => $account_id
            ),
            array(
                'ct.id' => $categoryTypeId
                , 'ct.parent_id' => $categoryTypeId
            )
        )
    )
));