drza10046 2016-09-09 00:37
浏览 96
已采纳

结合where - like - where_in

Let's say I have a model in CI returning what user(s) need to ..

$find = 'something';
$id_user = Array ( [0] => 1 [1] => 5 [2] => 20 [3] => 21 ) ;

So, I have to put that in here but something goes wrong ..

    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 () ;
    }

I get error Array to string conversion; input I have is from $find and $id_users that can be anything. I expected this logic, give me all array from table PR_DATA which is have %$find% on column ref_no or pr_title or req_date but only have $id_users *(1 or 5 or 20 or 21) in column req_by_id or check_by_id or approve_by_id.

Can anyone help?

  • 写回答

1条回答 默认 最新

  • dplm47571 2016-09-09 01:18
    关注

    This answer is under assumption that you need parenthesis in the first where_in() in your code.

    Unfortunately, CodeIgniter does not fully support parenthesis with Active Record. Therefore you will have to use where() with more complex SQL syntax in it.

    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 () ;
    }
    

    In CI's Active Record, this is how the syntax will be treated:

    The FIRST WHERE will be treated as: WHERE (req_by_id IN ($id_users_glued) || check_by_id IN ($id_users_glued) || approve_by_id IN ($id_users_glued)

    The $id_users_glued will produce something like 1,2,3,4,5

    The SECOND WHERE will be treated as: AND (ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')

    Note: I haven't tested the code because I don't have your db structure. Let me know if it's not working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效