Codeigniter's `where` and `or_where`

前端 未结 5 1671
旧时难觅i
旧时难觅i 2020-12-09 03:41

I\'m trying to specify a query in my model

$this->db
        ->select(\'*\')
        ->from(\'library\')
        ->where(\'library.rating >=\'         


        
相关标签:
5条回答
  • 2020-12-09 04:12

    You can modify just the two lines:

    ->where('(library.available_until >=', date("Y-m-d H:i:s"), FALSE)
    ->or_where("library.available_until = '00-00-00 00:00:00')", NULL, FALSE)
    

    EDIT:

    Omitting the FALSE parameter would have placed the backticks before the brackets and make them a part of the table name/value, making the query unusable.

    The NULL parameter is there just because the function requires the second parameter to be a value, and since we don't have one, we send NULL.

    0 讨论(0)
  • 2020-12-09 04:12

    You may group your library.available_until wheres area by grouping method of Codeigniter for without disable escaping where clauses.

    $this->db
        ->select('*')
        ->from('library')
        ->where('library.rating >=', $form['slider'])
        ->where('library.votes >=', '1000')
        ->where('library.language !=', 'German')
        ->group_start() //this will start grouping
        ->where('library.available_until >=', date("Y-m-d H:i:s"))
        ->or_where('library.available_until =', "00-00-00 00:00:00")
        ->group_end() //this will end grouping
        ->where('library.release_year >=', $year_start)
        ->where('library.release_year <=', $year_end)
        ->join('rating_repo', 'library.id = rating_repo.id')
    

    Reference: https://www.codeigniter.com/userguide3/database/query_builder.html#query-grouping

    0 讨论(0)
  • 2020-12-09 04:13
    $this->db->where('(a = 1 or a = 2)');
    
    0 讨论(0)
  • 2020-12-09 04:20

    You can use : Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow you to create queries with complex WHERE clauses. Nested groups are supported. Example:

    $this->db->select('*')->from('my_table')
            ->group_start()
                    ->where('a', 'a')
                    ->or_group_start()
                            ->where('b', 'b')
                            ->where('c', 'c')
                    ->group_end()
            ->group_end()
            ->where('d', 'd')
    ->get();
    

    https://www.codeigniter.com/userguide3/database/query_builder.html#query-grouping

    0 讨论(0)
  • 2020-12-09 04:28

    You can change your code to this:

    $where_au = "(library.available_until >= '{date('Y-m-d H:i:s)}' OR library.available_until = '00-00-00 00:00:00')";
    $this->db
        ->select('*')
        ->from('library')
        ->where('library.rating >=', $form['slider'])
        ->where('library.votes >=', '1000')
        ->where('library.language !=', 'German')
        ->where($where_au)
        ->where('library.release_year >=', $year_start)
        ->where('library.release_year <=', $year_end)
        ->join('rating_repo', 'library.id = rating_repo.id');
    

    Tip: to watch the generated query you can use

    echo $this->db->last_query(); die();
    
    0 讨论(0)
提交回复
热议问题