Kohana 3 ORM - grouping where conditions with parentheses

I am trying to run a query through ORM as follows:

SELECT * from table where (fname like 'string%' or lname like 'string%') AND (fname like 'string2%' or lname like 'string2%'); 

Here is what I still have:

 $results = ORM::factory('profiles'); foreach ($strings as $string) { $result->where('fname', 'like', "$string%"); $result->or_where('lname', 'like', "$string%"); } 

But this does not include parentheses. Any ideas?

+6
php orm kohana kohana-3 kohana-orm
source share
3 answers

Found the answer.

This is done with the Kohana where_open () and where_close () methods.

+8
source share

This works great for me.

Sample ORM Code

 $musicslist = ORM::factory('user_music') ->where_open() ->where('title', 'like', '%' . $search . '%') ->or_where('album', 'like', '%' . $search . '%') ->or_where('artist', 'like', '%' . $search . '%') ->where_close() ->and_where('app_userid','=', $userid) ->find_all(); 

he will create a SQL query

 SELECT `user_musics`.* FROM `user_musics` WHERE (`title` LIKE '%as%' OR `album` LIKE '%as%' OR `artist` LIKE '%as%') AND `app_userid` = '21' 
+7
source share

Failed to get the code formatting to work in the comment - I just thought I'd add a simple answer example if anyone else met it:

 $query = DB::select() ->from('some_table') ->where_open() ->where('column_one', '=', 1) ->or_where('column_two', '=', 2) ->where_close(); 

will create the following SQL:

 SELECT * FROM some_table WHERE (column_one = 1 OR column_two = 2); 
0
source share

All Articles