1

how do I build a find() query in cakePHP using these conditions:

Find where MyModel.x = 1 and MyModel.y = 2 OR MyModel.x = 1 and MyModel.y value does not exist (or is equal to empty string) 

Can somebody tell me how I can go about building such find query?

2 Answers 2

4

I'm gonna give you some pointers, but you need to try to do this as it's very basic and it's always good to practice.

A basic find in cake is in the form of

$this->ModelName->find('all'); 

This in its default form does a SELECT * from model_names (convention is to have singular ModelName for plural table name - model_names)

To add conditions:

$this->ModelName->find('all', array('conditions' => array('ModelName.x' => 1)); 

To add AND conditions

$this->ModelName->find('all', array('conditions' => array( 'ModelName.x' => 1, 'ModelName.y' => 2 )); 

To add OR conditions

$this->ModelName->find('all', array('conditions' => array( 'OR' => array( 'ModelName.x' => 1, 'ModelName.y' => 2 ) )); 

To combine both

$this->ModelName->find('all', array('conditions' => array( 'ModelName.y is not' => null, 'OR' => array( 'ModelName.x' => 1, 'ModelName.y' => 2 ) )); // where y is not null and (x = 1 or y = 2) 

http://book.cakephp.org/1.3/view/1030/Complex-Find-Conditions

(btw I'm sure there will be users giving you the exact answers, so just take my answer for your reference :) )

Sign up to request clarification or add additional context in comments.

Comments

3
$this->MyModel->find('all', array('conditions' => array( 'OR' => array( array( 'MyModel.x' => 1, 'MyModel.y' => 1 ), array( 'MyModle.x' => 1, 'OR' => array( array('MyModel.y' => NULL), array('MyModel.y' => '') ) ) ) ))); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.