sqlcakephpcakephp-2.0cakephp-2.1

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')));

Solution

  • 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