cakephp复杂查询多个'OR'条件 [英] cakephp complex query multiple 'OR' condition
本文介绍了cakephp复杂查询多个'OR'条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用cakephp进行这样的查询:
WHERE text LIKE'%keyword%'
AND
(
(text LIKE'%something%')
OR(text LIKE'%something%')
OR(...)
)
AND
(
(text LIKE'%other%')
OR(text LIKE'%other%')
OR(...)
)
NOT
(
(text LIKE'%dont include%')
OR(text LIKE'%dont include%')
OR(...)
)
这是我的$条件代码:
$ conditions = array
(
'Tweet.text LIKE'=>'%keyword%',
'AND' & array(
array(
'OR'=> array(
// topic
array('Tweet.text LIKE'=>'%something%') ,
array('Tweet.text LIKE'=>'%something%')
)
),
array(
'OR'=> array (
// sentiment
array('Tweet.text LIKE'=> '%other%'),
array('Tweet.text LIKE'=>'%other%')
)
)
),
'=> array(
array('Tweet.text LIKE'=>'%dont include%'),
array('Tweet.text LIKE'=>'%dont include%')
)
);
我使用Debugger :: dump()方法显示结果,结果只是使用最后'OR'条件,而不是两个'OR'条件:
array(
'Tweet.text LIKE' >'%keyword%',
'OR'=> array(
(int)0 => array(
'Tweet.text LIKE'=& '
),
(int)1 => array(
'Tweet.text LIKE'=>'%other%'
)
),
'NOT'=> array(
(int)0 => array(
'Tweet.text LIKE'=>'%dont include%'
),
(int)1 => array(
'Tweet.text LIKE'="'%dont include%'
)
)
)
我的问题是,如何进行一个查询,使用'OR'条件? $ b
$ b Pls尽快回复..)
解决方案 :
$ conditions = array(
'Tweet.text LIKE'=> '%aa%',//隐含和
数组(//隐含和
'或'=&array;数组(
array('Tweet.text LIKE'=>'%11 %b),
数组('Tweet.text LIKE'=>'%33%'),
数组('Tweet.text LIKE'=>'%22% $ b ...
)
),
array(//隐含和
'或'=&array; array(
array ='%123%'),
数组('Tweet.text LIKE'=>'%456%'),
数组('Tweet.text LIKE'=>'%789 %'),
...
)
)
'not'=> array(
'or'=> array(
array('Tweet.text LIKE'=>'%x%'),
array('Tweet.text LIKE'=> ;'%y%'),
array('Tweet.text LIKE'=>'%z%'),
...
)
)$ b b)
将是
text LIKE aa
AND(11,22,33)
AND(123,456,789)
BUT NOT(x || y || z )`
不指定或
,和
或不
是和
。无需手动指定。
i want to make query like this with cakephp:
WHERE text LIKE '%keyword%'
AND
(
(text LIKE '%something%')
OR (text LIKE '%something%')
OR (...)
)
AND
(
(text LIKE '%other%')
OR (text LIKE '%other%')
OR (...)
)
NOT
(
(text LIKE '%dont include%')
OR (text LIKE '%dont include%')
OR (...)
)
this is my code for $conditions:
$conditions = array
(
'Tweet.text LIKE' => '%keyword%',
'AND' => array(
array(
'OR' => array(
// topic
array('Tweet.text LIKE' => '%something%'),
array('Tweet.text LIKE' => '%something%')
)
),
array(
'OR' => array(
// sentiment
array('Tweet.text LIKE' => '%other%'),
array('Tweet.text LIKE' => '%other%')
)
)
),
'NOT' => array(
array('Tweet.text LIKE' => '%dont include%'),
array('Tweet.text LIKE' => '%dont include%')
)
);
i am displaying the result with Debugger::dump() method, and the result is just using the last 'OR' condition, not both 'OR' conditions:
array(
'Tweet.text LIKE' => '%keyword%',
'OR' => array(
(int) 0 => array(
'Tweet.text LIKE' => '%other%'
),
(int) 1 => array(
'Tweet.text LIKE' => '%other%'
)
),
'NOT' => array(
(int) 0 => array(
'Tweet.text LIKE' => '%dont include%'
),
(int) 1 => array(
'Tweet.text LIKE' => '%dont include%'
)
)
)
My question is, how do I make a query such that use both 'OR' condition?
Pls reply as soon as possible.. Thanks in advance :)
解决方案 Try the following:
$conditions = array(
'Tweet.text LIKE' => '%aa%', //implied and
array( //implied and
'or' => array(
array('Tweet.text LIKE' => '%11%'),
array('Tweet.text LIKE' => '%22%'),
array('Tweet.text LIKE' => '%33%'),
...
)
),
array( //implied and
'or' => array(
array('Tweet.text LIKE' => '%123%'),
array('Tweet.text LIKE' => '%456%'),
array('Tweet.text LIKE' => '%789%'),
...
)
)
'not' => array(
'or' => array(
array('Tweet.text LIKE' => '%x%'),
array('Tweet.text LIKE' => '%y%'),
array('Tweet.text LIKE' => '%z%'),
...
)
)
)
would be
text LIKE aa
AND ( either 11, 22, 33 )
AND (either 123, 456, 789)
BUT NOT (x || y || z)`
Any array that does not specify or
, and
or not
is and
. No need to specify it manually.
这篇关于cakephp复杂查询多个'OR'条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文