3

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();
$query->select('id');
$query->from('somecustomtable');
$query->orWhere(array(
    'somecolumn' => 'LIKE %foo%',
));
$query->orWhere(array(
    '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:

$query->orWhere(array(
    'somecolumn LIKE' => '%bar%',
));

This produces an error, however.

6

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

$query->orWhere(array(
    'like',
    'somecolumn',
    '%foo%'
));

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.