【问题标题】:Cleanest way to put condition inside CodeIgniter ActiveRecord将条件放入 CodeIgniter ActiveRecord 的最简洁方法
【发布时间】:2018-11-12 00:09:04
【问题描述】:

我需要在 CodeIgniter ActiveRecord 中添加条件,这是我的代码:

public function getData($numb, $period, $status){
    $this->db->select("COUNT(*) AS jml")
             ->from()
             ->where_not_in();
             if($status !== 'all'){
                $this->db->where("LAYANAN.TGL_LAYANAN BETWEEN DATEADD(" . $period ."," . $numb .", GETDATE()) AND GETDATE()");
             }
             $this->db->where_in()
             ->group_by("");
    return $query = $this->db->get();
}

我还需要将$period$numb 变量放在where 语句中。它可以工作,但我真的不喜欢我的代码的样子。它看起来很脏。你们能给我解决方案以实现更清晰的代码吗?谢谢

【问题讨论】:

    标签: sql-server codeigniter activerecord


    【解决方案1】:
    public function getData($numb, $period, $status){
    $this->db->select("COUNT(*) AS jml")
             ->from()
             ->where_not_in()
             ->where_in()
             ->group_by("");
    if($status !== 'all'){
        $this->db->where("LAYANAN.TGL_LAYANAN BETWEEN DATEADD(" . $period ."," . $numb .", GETDATE()) AND GETDATE()");
    }
    return $query = $this->db->get();
    

    }

    【讨论】:

      【解决方案2】:

      对整个字符串使用双引号,在 DATEADD 的参数周围使用单引号

      $this->db->where("LAYANAN.TGL_LAYANAN 
                        BETWEEN DATEADD(`$period`,`$numb`, GETDATE()) AND GETDATE()");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多