I'm trying to construct a fairly simple LIKE query using the Query Builder, and I'm struggling. Here's the SQL that I want:

SELECT `id` FROM `craft_somecustomtable` WHERE ((`somecolumn` LIKE '%foo%') OR (`somecolumn` LIKE '%bar%'))

I've ran the above query in Sequel, with results.

For the Query Builder, here's what I've got so far:

$query = craft()->db->createCommand();
    'somecolumn' => 'LIKE %foo%',
    'somecolumn' => 'LIKE %bar%',

This produces the following query, which obviously doesn't work as intended:

SELECT `id` FROM `craft_somecustomtable` WHERE ((`somecolumn`='LIKE %foo%') OR (`somecolumn`='LIKE %bar%'))

I've also tried adding the LIKE directive like this:

    'somecolumn LIKE' => '%bar%',

This produces an error, however.


Just before posting the question, I had the good mind to read the Yii CDbCommand docs again, and I figured this out myself:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.