Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter's `where` and `or_where`

Tags:

I'm trying to specify a query in my model

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

So, the trouble i'm having is with my or_where. I want the or to be restricted to only the available_until field. Currently, however, i'm getting results which have a language of German which isn't what i want. How do i restrict my or_where filter to the available_until field only?

like image 889
garethdn Avatar asked May 11 '13 15:05

garethdn


People also ask

Where is the code igniter?

CodeIgniter Select Query with $this->db->where_in()The where_in() function is used to generate WHERE field IN ('item', 'item') SQL query string joined with AND if appropriate.

How do you check data is updated or not in CodeIgniter?

Mirov When we are working with codeigniter, the data only updated when there is some change in the input field's value and then the $this->db->affected_rows() will return value greater then 0.

How do I print a query in CI?

Use the below query to display the query string: print_r($this->db->last_query()); To display the query result follow this: print_r($query);


2 Answers

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.

like image 112
Yan Berk Avatar answered Sep 23 '22 13:09

Yan Berk


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

like image 38
MERT DOĞAN Avatar answered Sep 21 '22 13:09

MERT DOĞAN