简体   繁体   English

cakephp匹配订单不起作用

[英]cakephp match against order by doesn't work

I am trying to order my pagination result. 我正在尝试订购我的分页结果。 But it doesn't work. 但它不起作用。

$this->paginate = array(
                'limit' => 15,
                'fields' => array('*', "MATCH (headline, author, short_headline, subline, teaser, text) AGAINST ('$var') AS score"),
                'conditions' =>  "MATCH(headline, author, short_headline, subline, teaser, text) AGAINST('$var' IN BOOLEAN MODE)",
                'order' => array('score' => 'desc')
            );

The query looks like: 查询看起来像:

SELECT *, MATCH (`headline`, `author`, `short_headline`, `subline`, `teaser`, `text`) AGAINST ('herz tod') AS `score`, `Story`.`id` FROM `fates`.`stories` AS `Story` WHERE MATCH(headline, author, short_headline, subline, teaser, text) AGAINST('herz tod' IN BOOLEAN MODE) LIMIT 15

and order by score is gone! 按分数排序消失了! What i am doing wrong? 我做错了什么? thanks for help! 感谢帮助!

Try making score a virtualField before you try to paginate in order to use it to order your pagination. 在尝试分页之前尝试将分数设为虚拟字段,以便使用它来分页。 (There may be other ways to get it to order by score, but I'm not aware of them.) (可能还有其他方法可以按分数排序,但我不知道它们。)

$this->Model->virtualFields['score'] = "MATCH (headline, author, short_headline, subline, teaser, text) AGAINST ('" . Sanitize::escape($var) . "')";

I'd like to also point out, just in case, that your code (and the code I posted above for that matter) is vulnerable to SQL injection, if you cannot assume $var to be trustworthy data, like if $var comes from user input. 我还想指出,以防万一,你的代码(以及我上面发布的代码)容易受到SQL注入的影响,如果你不能假设$var是值得信赖的数据,比如$var来自用户输入。

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

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