hellosheikh
hellosheikh

Reputation: 3015

like OR operator in Cakephp

I am new to cakephp and I want to add or, and, and like to my existing query.

I want to make a condition like this

WHERE  'Message.user_id = Contact.user_id' AND 'Message.mobileNo LIKE'=>"%Contact.mobileNo" OR LIKE'=>"%Contact.homeNo" OR LIKE'=>"%Contact.workNo"

My query is

$this->bindModel(array(
        'belongsTo' => array(
            'Contact' => array(
                'className' => 'Contact',
                'foreignKey' => false,
                'conditions' => array(
                    'Message.user_id = Contact.user_id',
                    'Message.mobileNo = Contact.mobileNo'
                ),
                'type' => 'inner'
            )
        )
    ), false); 

      $this->find('all', array('conditions' => array(),
        'fields' => array('DISTINCT mobileNo')));

Upvotes: 0

Views: 2825

Answers (2)

liyakat
liyakat

Reputation: 11853

you can use like below in your existing query.

$this->find('all', array(
    'conditions' => array(
        'OR' => array(
            array(
                "Message.mobileNo LIKE" => "%Contact.mobileNo",
            ),
            array(
                "Message.mobileNo LIKE" => "%Contact.homeNo",
            ),
            array(
                "Message.mobileNo LIKE" => "%Contact.workNo",
            )
        )
    ),
    'fields' => array('DISTINCT mobileNo')
));

And you can also refer Detail Document for simple search with like

Upvotes: 1

Digital Alchemist
Digital Alchemist

Reputation: 2332

you can use: for "like"

$this->Post->find("all",array('condition'=>array('Author LIKE'=>"ad%")));

above query will give You the data from table posts where author name starts with "ad".

for "OR"

$this->Post->find("all",array('condition'=>array("OR"=>array('Author LIKE'=>"ad%",'Author LIKE'=>"bo%"))));

above query will give You the data from table posts where author name starts with "ad" OR "bo"

This blog post can b useful to u as well!

Upvotes: 0

Related Questions