Collections Magento2在collection->setOrder中添加自定义值

Collections Magento2在collection->setOrder中添加自定义值,collections,magento2,Collections,Magento2,我需要在集合的setOrder中添加一些自定义值,但它不起作用 $collection->setOrder("((lat - ".$lat.")*(lat - ".$lat.")) + ((lng - ".$lng.")*(lng - ".$lng."))", 'ASC'); 我的集合查询的最后一部分如下所示: ORDER BY ((lat - -34.9226513)*(lat - -3

我需要在集合的setOrder中添加一些自定义值,但它不起作用

$collection->setOrder("((lat - ".$lat.")*(lat - ".$lat.")) + ((lng - ".$lng.")*(lng - ".$lng."))", 'ASC');
我的集合查询的最后一部分如下所示:

ORDER BY ((lat - -34.9226513)*(lat - -34.9226513)) + ((lng - 138.6094486)*(lng - 138.6094486)) ASC

您应该尝试去掉$lat和$lng变量中的负号,以便正确地连接它们

像这样的东西没有经过测试

$latString = '(lat'. (($lat >= 0) ? '-' : '+').abs($lat).')';
$lngString = '(lng'. (($lng >= 0) ? '-' : '+').abs($lng).')';
$collection->setOrder($latString.'*'.$latString.'+'.$lngString.'*'.$lngString, 'ASC');