Domanda

Sto cercando di eseguire una query tramite l'ORM in questo modo:

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

Ecco quello che ho finora:

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

Ma questo non tiene conto per le parentesi. Tutte le idee?

È stato utile?

Soluzione

Trovato la risposta.

E 'fatto con Kohana di where_open () e where_close () metodi.

Altri suggerimenti

Funziona bene per me.

di codice ORM

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

si creerà query SQL

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

Non è stato possibile ottenere il codice di formattazione al lavoro nel commento - Ho pensato di aggiungere un semplice esempio per la risposta nel caso in cui nessun altro si imbatte in esso:

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

dovrebbe produrre il seguente SQL:

SELECT * FROM some_table
WHERE (column_one = 1 OR column_two = 2);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top