I'm having a hard time to translate the following query into kohana's ORM.
我很难将以下查询翻译成kohana的ORM。
So, if I do the following works fine:
所以,如果我做以下工作正常:
$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');
However, I need to use an specific class model. So far I have:
但是,我需要使用特定的类模型。到目前为止我有:
$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);
Which works fine, except for the fact that I'm unable to use the score value. I need the score because since I changed the table engine to InnoDB, the 2nd query is returning a lot of results.
哪个工作正常,除了我无法使用得分值这一事实。我需要得分,因为我将表引擎更改为InnoDB,第二个查询返回了很多结果。
ORM here: https://github.com/kohana/orm/blob/3.3/master/classes/Kohana/ORM.php
ORM在这里:https://github.com/kohana/orm/blob/3.3/master/classes/Kohana/ORM.php
Thank you for your time.
感谢您的时间。
1 个解决方案
#1
1
So, you don't use query builder, but ORM object finding. In first case you take result array on second array of objects.
因此,您不使用查询构建器,而是使用ORM对象查找。在第一种情况下,您将结果数组放在第二个对象数组上。
Trust me, you don't want use list objects. (It's extremely slow)
相信我,你不想使用列表对象。 (它非常慢)
$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();
for query test:
用于查询测试:
die($query->compile(Database::instance()));
OT: Use
OT:使用
$books = ORM::factory('Book')->full_text($search_terms, $score);
instead $books = new Model_Book();
相反,$ books = new Model_Book();
#1
1
So, you don't use query builder, but ORM object finding. In first case you take result array on second array of objects.
因此,您不使用查询构建器,而是使用ORM对象查找。在第一种情况下,您将结果数组放在第二个对象数组上。
Trust me, you don't want use list objects. (It's extremely slow)
相信我,你不想使用列表对象。 (它非常慢)
$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();
for query test:
用于查询测试:
die($query->compile(Database::instance()));
OT: Use
OT:使用
$books = ORM::factory('Book')->full_text($search_terms, $score);
instead $books = new Model_Book();
相反,$ books = new Model_Book();