Sign up ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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%'
));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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