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.