2017-08-23 68 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