Yii查询生成器Where LIKE子句


Yii Query Builder Where LIKE clause

我被这个代码卡住了:

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

这是我想要的

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

我试了很多组合都没有结果。

请帮帮我。谢谢

我认为问题是CDbCommandwhere()方法不支持在$conditions数组中使用键值对,就像您在这里所做的那样:'id_i'=>$model->id_e。该绑定需要通过$params参数进行。

除此之外,我还建议使用带值绑定的字符串,而不是数组语法,因为这会使代码更容易阅读。

以下是对where():调用的修改代码

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

您可以看到它更接近最终的SQL,这使得调试更容易。

如果你决定使用原始语法,可以试试这个:

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

尝试将子句放在where()方法上,放在CDbCommon的类上,其中()方法指定查询的where部分,第一个参数是查询条件,第二个参数是要绑定到查询的参数(name=>value)。

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

有关CDbCommand where()方法的更多详细信息