【问题标题】:Yii Query Builder Where LIKE clauseYii Query Builder Where LIKE 子句
【发布时间】:2014-10-06 10:05:00
【问题描述】:

我被这段代码卡住了:

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

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

请帮助我。 谢谢。

【问题讨论】:

    标签: php yii where sql-like


    【解决方案1】:

    我认为问题在于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))
    

    【讨论】:

      【解决方案2】:

      尝试在 CDbCommand 类的 where() 方法上放置子句, where() 方法指定查询的 WHERE 部分,第一个参数是查询条件,第二个参数是要绑定的参数(名称 => 值)到查询。

      ->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() 方法here的细节

      【讨论】:

      • 嘿嘿,别忘了说明你做了什么改变以及为什么。不要忘记 Stack Overflow 上有很多新手可以从你的经验中学到一两件事,而对你来说显而易见的东西可能对他们来说不是。
      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多