简体   繁体   English

Yii Query Builder Where 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. 谢谢。

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 . 我认为问题在于CDbCommandwhere()方法不支持 $conditions数组中使用键-值对就像在这里做的那样: 'id_i'=>$model->id_e That binding needs to happen through the $params parameter. 该绑定需要通过$params参数进行。

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()的修改后的代码:

->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. 您会看到它更接近最终的SQL,这使调试更加容易。

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

try to put clause on where() method, on class of CDbCommand, where() method specifies the WHERE part of query, the first argumnet are query condition and second argument are the parameters (name => value) to be bound to the query. 尝试在CDbCommand的类上的where()方法上放置子句,where()方法指定查询的WHERE部分,第一个argumnet是查询条件,第二个参数是要绑定到查询的参数(名称=>值) 。

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

more details about CDbCommand where() method here 有关CDbCommand where()方法的更多详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM