php CakePHP 查询 - 复杂的 AND/OR 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18957242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP query - complex AND/OR conditions
提问by harryg
I'm trying to get my head around the complex find conditions of CakePHP and have read the docsbut am struggling with this one query.
我正在尝试了解 CakePHP 的复杂查找条件并阅读了文档,但我正在为这个查询而苦苦挣扎。
SELECT field1,
field2
WHERE id = 123456
AND ((holding_date = Last_day(holding_date)
AND Month(holding_date) IN(3, 6, 9, 12))
OR (holding_date = '2013-09-15'))
To produce the above conditions what would my conditions
array look like?
要产生上述条件,我的conditions
数组会是什么样子?
回答by AD7six
CakePHP conditions and sql expressions
CakePHP 条件和 sql 表达式
While the conditions in the question are not that complex, they touch on a few points which mean they can be tricky to define correctly. Some of the things to know when defining cakephp conditions:
虽然问题中的条件并不那么复杂,但它们涉及一些要点,这意味着它们可能很难正确定义。定义 cakephp 条件时需要了解的一些事项:
- Conditions are defined as an array of key => value pairs, as such the same key cannot be defined twice on the same level
- an array element which has a numeric key is interpreted as an sql expression
- The default join mode is "AND" - it's not necessary to specify
"AND" => ...
in conditions - An OR conditions must have more than one elements. There's no error if it has only one but otherwise: OR what?
- 条件被定义为键 => 值对的数组,因此同一个键不能在同一级别上定义两次
- 具有数字键的数组元素被解释为 sql 表达式
- 默认连接模式是“AND” - 不需要
"AND" => ...
在条件中指定 - OR 条件必须具有多个元素。如果只有一个就没有错误,否则:或者什么?
Bearing in mind the above notes, the conditions in the question can be expressed as:
牢记上述注释,问题中的条件可以表示为:
$foo->find('all', array(
'fields' => array(
'field1',
'field2'
),
'conditions' => array(
'id' => 123456,
'OR' => array(
array(
'holding_date = LAST_DAY(holding_date)',
'MONTH(holding_date)' => array(3,6,9,12)
),
'holding_date' => '2013-09-15'
)
)
));
Which results in:
结果是:
WHERE
`id` = 123456
AND
(
(
(holding_date = LAST_DAY(holding_date))
AND
(MONTH(holding_date) IN (3, 6, 9, 12)))
)
OR
(`holding_date` = '2013-09-15')
)
Note: whitespace is quite important =) I misread the question originally solely because of the inconsistent whitespace in the question's sql.
注意:空格非常重要 =) 我最初只是因为问题的 sql 中的空格不一致而误读了这个问题。
回答by harryg
OK I have solved it:
好的,我已经解决了:
$findParams['conditions'] = array(
'Account.client_id' => '12345',
'AND' => array(
'OR' => array(
'Holding.holding_date' => '2013-09-15',
'AND' => array(
'Holding.holding_date = LAST_DAY(Holding.holding_date)',
'MONTH(Holding.holding_date)' => array(3,6,9,12)
)
)
)
);
回答by lukasz
Try this:
尝试这个:
$params['conditions'] = array(
'`id`' => 123456,
'AND' => array(
'`holding_date`' => 'LAST_DAY(`holding_date`)',
'AND' => array(
'MONTH(holding_date)' => array(3, 6, 9, 12),
'OR' => array(`holding_date` => '2013-09-15')
)
)
);