简体   繁体   English

使用Kohana的ORM建立MySQL查询

[英]MySql Query build using Kohana's ORM

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. 感谢您的时间。

So, you don't use query builder , but ORM object finding . 因此,您无需使用查询生成器 ,而是使用ORM对象查找 In first case you take result array on second array of objects. 在第一种情况下,将结果数组放在第二个对象数组上。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM