cakephp在条件中找到多组OR

时间:2015-02-10 19:04:01

标签: mysql cakephp

我看到另一个问题在技术上回答了我的回答,但我的查询无效。我认为查询是自解释的,但它没有返回任何行。

CakePHP find conditions with multiple 'OR'

$sector_info['Notam'] = $this->Notam->find('all', array(
                    'group' => 'Notam.id',
                    'conditions' => array(
                        'AND' => array(
                            'OR' => array(
                                'Notam.notam_expire_dtg < '.date("YmdHis"),
                                'Notam.notam_expire_dtg' => '',
                                ),
                            'OR' => array(
                                'Notam.account_id' => $sector_info['Airport']['identifier'],
                                'Notam.cns_location_id' => $sector_info['Airport']['identifier'],
                                'Notam.account_id' => $sector_info['Airport']['icao'],
                                'Notam.cns_location_id' => $sector_info['Airport']['icao'],
                                ),
                            ),
                        ),
                    )
                );

我想拉(不过期,或没有过期)和(或其他4个条件。)

非常感谢,  莱恩

编辑:这是输出的查询,根本没有提到Notam.notam_expire_dtg:

SELECT `Notam`.`id`, `Notam`.`key`, `Notam`.`source_id`, `Notam`.`account_id`, `Notam`.`notam_id`, `Notam`.`notam_nrc`, `Notam`.`xoveraccountid`, `Notam`.`xovernotamid`, `Notam`.`notam_part`, `Notam`.`cns_location_id`, `Notam`.`icao_id`, `Notam`.`icao_name`, `Notam`.`total_parts`, `Notam`.`notam_effective_dtg`, `Notam`.`notam_expire_dtg`, `Notam`.`notam_lastmod_dtg`, `Notam`.`notam_text`, `Notam`.`notam_report`, `Notam`.`notam_qcode`, `Notam`.`pull`, `Notam`.`dflag`, `Notam`.`sql_update` FROM `aaid_cake`.`notams` AS `Notam` WHERE ((`Notam`.`account_id` IN ('PABE', 'BET')) OR (`Notam`.`cns_location_id` IN ('PABE', 'BET'))) GROUP BY `Notam`.`id`

2 个答案:

答案 0 :(得分:2)

php基础:您无法拥有重复的数组键,您的第二个['OR']声明将覆盖第一个。这应该会让你的错误显而易见。

试试这个:

'conditions' => [
    'AND' => [
        ['OR' => [/*...*/]],
        ['OR' => [/*.../*]]
    ]
 ]

答案 1 :(得分:1)

你可以试试这个:

$sector_info['Notam'] = $this->Notam->find('all', array(
                'group' => 'Notam.id',
                'conditions' => array(
                    'OR' => array(
                            'Notam.notam_expire_dtg < '.date("YmdHis"),
                            'Notam.notam_expire_dtg' => '',
                     ),
                    'AND'=>array(
                     'OR' => array(
                            'Notam.account_id' => $sector_info['Airport']['identifier'],
                            'Notam.cns_location_id' => $sector_info['Airport']['identifier'],
                            'Notam.account_id' => $sector_info['Airport']['icao'],
                            'Notam.cns_location_id' => $sector_info['Airport']['icao'],
                            )
                        ),                            
                    ),
                )
            );