Question

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

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

but it make this query:

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

how to replace AND with OR?

thanks

Was it helpful?

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%')))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top