Wednesday, April 3, 2013

Re: Problem with nested AND / OR Condition

AND is the default way in which adjacent query sections are joined together - unless inside an array keyed with one of the other sql operators (OR, NOT, AND NOT etc.), where it is that key.

What you have written in the question is attempting to do, more or less, this (for illustration, not the actual query generated):

WHERE id = 1 AND (column = 'test1' AND id = 2) AND (column = 'test2' AND id = 3)
             ^                                 ^                                // From where AND is written in the question
                                   ^                                 ^          // Implicit

Just remove the AND keys:

$options = array(
'conditions' => array(
'OR' => array(
'id' => 1,
array(
'column1' => 'test1',
'id' => 2
),
array(
'column1' => 'test2',
'id' => 3
)
)
)
);

Which will generate:

WHERE id = 1 OR (column = 'test1' AND id = 2) OR (column = 'test2' AND id = 3)

AD

On Saturday, 30 March 2013 03:02:02 UTC+1, Carsten Wawer wrote:
Hey Folks

maybe I'm just at a loss, but how do I create a SQL command like this with cakePHP?

SELECT *
FROM table
WHERE id = 1 OR (column1='test1' AND id=2) OR (column1='test2' AND id=3)


Please do not ask about the meaning, I have simplified a more complex command to make the problem clear.

The Condition would have to actually something like this:


$options = array(
'conditions' => array(
'OR' => array(
'id' => 1,
'AND' => array(
'column1' => 'test1',
'id' => 2
),
'AND' => array(
'column1' => 'test2',
'id' => 3
)
)
)
);

But the second 'AND' overrwrites the first one.

Can someone give me a hint? I would be grateful! ;-)

Greetings!
Carsten

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: