如何在Drupal 8中按英里扩展给定的查询搜索?



我已经给出了节点的查询,并希望将搜索扩展为英里,

$query = Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'event')
->range(0, 10);

使用以下查询(已安装的地理位置模块)以数组格式出现的位置,并希望使用自定义查询进行记录,而不使用内部搜索功能

$locationInArray = $node->get('location')->getValue();

数组值如下所示:

纬度, 液化天然气(经度), lat_sin(预先计算的正弦纬度), lat_cos(预先计算的纬度余弦), lng_rad(预先计算的弧度经度)。

我遇到了来自另一个表的经度问题,所以如何扩展上面的查询

我需要距离明智的搜索,如 2 公里/英里,无需 drupal 模块的帮助 因为我已经实现了它,但不能满足我的所有需求,所以想制作自定义搜索模块。

这是给定的距离查询,但我是 Drupal 的新手,所以请帮助我将给定的查询集成到 Drupal 我的代码中。

选择 同上,( 3959 * ACOS ( Cos ( 弧度(78.3232) ) * cos( 弧度( 纬度 ) ) * COS( 弧度( 液化天然气 ) - 弧度(65.3234) ) + 罪 ( 弧度(78.3232) ) * sin( 弧度( lat ) ) ) ) 作为距离 从标记 距离<30 按距离排序 限制 0 , 20;

你能试试:

$query = Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'event')
->condition('location.lat', $your_value_lat)
->condition('location.lng', $your_value_lng)
->condition('location.lat_sin', $your_value_lat_sin)
->condition('location.lat_cos', $your_value_lat_cos)
->condition('location.lng_rad', $your_value_lng_rad)
->range(0, 10);

问题更改后编辑:

要在 drupal 中使用查询 directyl,请使用$query = Drupal::database()->query()

更多: https://www.drupal.org/docs/8/api/database-api

您的示例应该看起来像这样(我只是用变量交换一个值来显示如何做到这一点):

$query = Drupal::database()
->query("SELECT id, ( :example_variable * acos ( cos ( radians(78.3232) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(65.3234) ) + sin ( radians(78.3232) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 30 ORDER BY distance LIMIT 0 , 20;",
[
':example_variable' => '3959'
]);
$result = $query->fetchAll();

最新更新