我试图在此处插入的原始查询是:
SELECT * FROM x WHERE CONCAT(y, ' ', x) LIKE '%value%';
我已经查看了 AR 文档,但找不到任何允许我执行此操作的内容。我不太熟悉它是如何构造这些查询的,希望有人能给我指出正确的方向。非常感谢。
最佳答案
如果你想使用 AR 类,你需要将 FALSE 作为第三个参数传递,以避免查询被自动转义。你现在只能自己逃避争论了:
$value = $this->db->escape_like_str($unescaped);
$this->db->from('x');
$this->db->where("CONCAT(y, ' ', x) LIKE '%".$value."%'", NULL, FALSE);
$result = $this->db->get();
引用手册Active Record session 中的第4)点。引用:
Custom string:
You can write your own clauses manually:
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
恕我直言,更简单的方法是运行“常规”查询并利用绑定(bind):
$result = $this->db->query("CONCAT(y, ' ', x) LIKE '%?%'", array($value));
关于php - 在 CodeIgniter 中使用 WHERE CONCAT 和 Active Record,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14781808/