假设我在CI中有一个模型,返回用户需要的内容。。
$find = 'something';
$id_user = Array ( [0] => 1 [1] => 5 [2] => 20 [3] => 21 ) ;
所以,我必须把它放在这里,但出了问题。。
public function find_pr_by_user ($find,$id_users) {
$this -> db -> where_in ('req_by_id'.$id_users || 'check_by_id',$id_users || 'approve_by_id',$id_users);
$this -> db -> where ("(ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')");
$query = $this -> db -> get ('pr_data') ;
return $query -> result () ;
}
我得到错误数组到字符串的转换;我的输入来自CCD_ 1和CCD_。我期望这个逻辑,给我来自表PR_DATA
的所有数组,它在列ref_no
、pr_title
或req_date
上有%$find%
,但在列req_by_id
、$find
0或approve_by_id
上只有$id_users
*(1或5、20或21(。
有人能帮忙吗?
这个答案是假设您在代码的第一个where_in()
中需要括号。
不幸的是,CodeIgniter并不完全支持带活动记录的括号。因此,您将不得不使用where()
,其中包含更复杂的SQL语法。
public function find_pr_by_user ($find,$id_users) {
$id_users_glued = implode(",",$id_users);
$this->db->where('(req_by_id IN (' . $id_users_glued . ') || check_by_id IN (' . $id_users_glued . ') || approve_by_id IN (' . $id_users_glued . '))');
$this->db->where("(ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')");
$query = $this->db->get('pr_data') ;
return $query->result () ;
}
在CI的活动记录中,以下是语法的处理方式:
第一个位置将被视为:WHERE (req_by_id IN ($id_users_glued) || check_by_id IN ($id_users_glued) || approve_by_id IN ($id_users_glued)
$id_users_glued
将产生类似1,2,3,4,5
的
第二个位置将被视为:AND (ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')
注意:我没有测试代码,因为我没有您的数据库结构。如果不起作用,请告诉我。