Search code examples
phpyiiwhere-clausesql-like

Yii Query Builder Where LIKE clause


i am stuck with this code:

->select ('t1.id_i, t2.status')
->from ('table1 as t1, table2 as t2')           
->where(array('or', 'id_i'=>$model->id, array('like', 't2.status', '%Beginner%')))

here are what i want

 WHERE t1.id_i=$model->id AND/OR t2.status LIKE "Beginner" //AND/OR are optional combination

i tried a lot combination with no result.

Please help me. Thanks.


Solution

  • I think the problem is that CDbCommand's where() method doesn't support using key-value pairs within the $conditions array as you're doing here: 'id_i'=>$model->id_e. That binding needs to happen through the $params parameter.

    That aside, I'd also recommend using a string with value binding instead of the array syntax, as it will make the code easier to read.

    Here's the modified code for the call to where():

    ->where('id_i = :id_e OR t2.status LIKE "%Beginner%"', array(':id_e' => $model->id_e))
    

    You can see that it's closer to the final SQL, which makes debugging easier.

    If you're determined to use the original syntax, try this instead:

    ->where(array('or', 'id_i = :id_e', array('like', 't2.status', '%Beginner%')), array(':id_e' = $model->id_e))