2017-08-23 60 views
0

我很難將以下查詢翻譯成kohana的ORM。使用Kohana的ORM構建MySql查詢

所以,如果我做了以下工作正常:

$query = DB::query(Database::SELECT, 'SELECT id_book, MATCH(title, author, isbn) AGAINST (:str) AS score FROM tab_books WHERE status = 1 AND MATCH(title, author, isbn) AGAINST (:str) HAVING score > '.$score.' ORDER BY score DESC LIMIT 100'); 

不過,我需要使用特定的類模型。到目前爲止,我有:

$books = new Model_Book(); 
$books = $books->where('status', '=', 1); 
$books = $books->where(DB::expr('MATCH(`title`,`author`,`isbn`)'), 'AGAINST', DB::expr("(:str)"))->param(':str', $search_terms); 

這工作正常,除了我無法使用得分值的事實。我需要得分,因爲自從我將表引擎更改爲InnoDB後,第二個查詢返回了很多結果。

ORM這裏:https://github.com/kohana/orm/blob/3.3/master/classes/Kohana/ORM.php

謝謝您的時間。

回答

1

所以,你不使用query builder,但ORM object finding。 在第一種情況下,您將結果數組放在第二個對象數組上。

相信我,你不想使用列表對象。 (這是非常緩慢)

$sq = DB::expr('MATCH(title, author, isbn) AGAINST (:str) AS score') 
     ->param(":str", $search_terms); 
$wq = DB::expr('MATCH(title, author, isbn)'); 
$query = DB::select('id_book', $sq) 
    ->from('tab_books') // OR ->from($this->_table_name) for model method 
    ->where('status','=',1) ->where($wq, 'AGAINST ', $search_terms) 
    ->order_by('score', desc)->limit(100) //->offset(0) 
    ->having('score', '>', $score); 
$result = $query->execute()->as_array(); 

查詢測試:

die($query->compile(Database::instance())); 

OT:使用

$books = ORM::factory('Book')->full_text($search_terms, $score); 

代替$books = new Model_Book();

+0

謝謝bato3。 – Raphael