【问题标题】:MySql Query build using Kohana's ORMMySql Query 使用 Kohana 的 ORM 构建
【发布时间】:2017-08-23 20:43:40
【问题描述】:

我很难将以下查询转换为 kohana 的 ORM。

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

$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

感谢您的宝贵时间。

【问题讨论】:

    标签: mysql kohana kohana-orm kohana-3.3


    【解决方案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();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多