Search code examples
phpmysqllithium

lithium framework how to make a query with OR


I start work with lithium PHP framework. I have to make a query to get questions which have '%test%' or '%easy%' in 'title' field. I've tried to do it using following code:

$questions = Questions::find('all', array(
        'conditions'    => array(
            'name' => array(
                'like' => array(
                    '%easy%',
                    '%test%'
                )
            )
        )
    ));

but it makes this query:

SELECT * FROM `questions` AS `Questions` WHERE (`name` LIKE '%easy%' AND `name` LIKE '%test%')

how to replace AND with OR?


Solution

  • as a solution to your question you can use in (=) instead of like

    'conditions'    => array(
        'name' => array(
            '=' => array(
                '%easy%',
                '%test%'
            )
        )
    )
    

    this will generate the query:

    WHERE ((`name` IN ('%easy%', '%test%')))
    

    Or can be used when searching using two different fields:

    'conditions' => array(
        'OR' => array(
            'name' => array(
                '=' => array(
                    '%easy%',
                    '%test%'
                )
            ),
            'name2' => array(
                '!=' => array(
                    '%easy%',
                    '%test%'
                )
            )
        )
    ),
    

    And this will generate query:

    WHERE ((`name` IN ('%easy%', '%test%')) OR (`name2` IN ('%easy%', '%test%')))