简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM