It’s a bug in Cakephp - Sort Aliases?

Asked

Viewed 133 times

0

I’m trying to sort a query by relevance with cakephp using Paginator, but simply it ignores the alias, I already searched the manual but so far I haven’t found the solution, the query works perfectly, when I debug generates the query below, but the order by does not appear see:

    SELECT *, MATCH (`titulo`,`conteudo`) AGAINST ('e-mail marketing' IN BOOLEAN MODE) AS relevance, MATCH (`titulo`) AGAINST ('e-mail marketing' IN BOOLEAN MODE) AS title_relevance FROM `db_forum`.`ra_forum` AS `Forum` WHERE MATCH (titulo,conteudo) AGAINST ('e-mail marketing' IN BOOLEAN MODE) LIMIT 10

Follows the code:

$this->paginate = array(

                'fields'     => array('*', "MATCH (`titulo`,`conteudo`) AGAINST ('$q' IN BOOLEAN MODE) AS relevance,  MATCH (`titulo`) AGAINST ('$q' IN BOOLEAN MODE) AS title_relevance"),
                'conditions' =>  "MATCH (`titulo`,`conteudo`) AGAINST ('$q' IN BOOLEAN MODE)",
                'order'      => array( 'relevance' => 'desc', 'title_relevance' => 'desc'),
                'limit'      => 10
        );

Some trick to make it work?

Edit:

Tip for those who need, I was putting as parameters, the correct is:

'order'      => 'relevance desc, title_relevance desc',
  • I may be wrong, but I think 'order' only accepts one parameter. You’re trying to order using two things

  • I found the error, no need to go through array is direct!

  • Actually, it is not correct, you need to reference the model at the beginning of the array, or in each field.

1 answer

1


The correct thing is to combine the model with the field.

array(
    'conditions' => array('Model.field' => $thisValue), //array of conditions
    'recursive' => 1, //int
    //array com os campos
    'fields' => array('Model.field1', 'DISTINCT Model.field2'),
    //string(1 campo) ou array(mais de 1 campo)
    'order' => array('Model.created', 'Model.field3 DESC'),
    'group' => array('Model.field'), //fields to GROUP BY
    'limit' => n, //int
    'page' => n, //int
    'offset' => n, //int
    'callbacks' => true //ou false, 'before', 'after'
);

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

This rule also applies to PaginatorComponent

You can use this answer: Pagination always shows 20 images

Browser other questions tagged

You are not signed in. Login or sign up in order to post.