How to add Order priority?

I use FOC Search plugin, and setup two methods with fulltext search, where result is sorted by virtual relevance field.
On the controller i have one more ‘order()’ method.

Problem, generated query looks like:

ORDER BY 
  Location__relavance DESC,  // FOC Search
  Posts__relavance DESC,  // FOC Search
  Posts.active DESC  // PostsController

How to set Order priority, like?

ORDER BY 
  Posts.active DESC  // PostsController
  Location__relavance DESC,  // FOC Search
  Posts__relavance DESC,  // FOC Search

I’m guessing you could have the order in the pagination properties, but maybe it’ll be overwritten

or do something like this

use Cake\Database\Expression\OrderByExpression;

$originalOrder = $query->clause('order');
$newOrder = new OrderByExpression();
$newOrder->add(['Posts.active' => 'DESC']);
$newOrder->add($originalOrder);
$query->order($newOrder, true);

I didn’t try this, but should work

1 Like