【问题标题】:CakePHP find all condition being ignoredCakePHP 发现所有条件都被忽略
【发布时间】:2013-10-10 19:13:56
【问题描述】:

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

这里涉及到三个表,products、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
        )
    )
));

问题是查询忽略了 account_id OR 条件,导致 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 ((ct.id = '1') OR (ct.parent_id = '1'))

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

【问题讨论】:

标签: cakephp


【解决方案1】:

感谢 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
            )
        )
    )
));

【讨论】:

    【解决方案2】:

    我认为,您应该将两个数组都包装在一个 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
                )
            )
        )
    ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 2019-06-02
      • 2016-02-09
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 2021-08-04
      • 2019-09-11
      相关资源
      最近更新 更多