【问题标题】:How to extend given query for search by miles in Drupal 8?如何在 Drupal 8 中扩展给定查询以按英里搜索?
【发布时间】:2019-11-26 00:44:54
【问题描述】:

我已经对节点进行了查询,并希望以英里为单位扩展搜索,

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

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

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

数组值如下:

lat(纬度), lng(经度), lat_sin(预先计算的纬度正弦), lat_cos(预先计算的纬度余弦), lng_rad(预先计算的弧度经度)。

我面临来自另一个表的经纬度问题,因此如何扩展上述查询

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

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

选择 ID, ( 第3959章 cos ( 弧度 (78.3232) ) * cos( 弧度( lat ) ) * cos(弧度(lng)-弧度(65.3234)) + 罪 ( 弧度 (78.3232) ) * 罪(弧度(纬度)) ) ) 作为距离 FROM 标记 距离

【问题讨论】:

  • 你能确认 'location' 实际上不是 'field_location' 吗?
  • field_location 位置来自另一个从地理定位模块创建的表
  • 是的,如果 drupal 有自己的表,则每个字段。我只是想确定$locationInArray = $node->get('location')->getValue(); 不是$locationInArray = $node->get('field_location')->getValue();:)

标签: drupal geolocation drupal-8 query-builder


【解决方案1】:

你可以试试:

$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中使用查询直接使用$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();

【讨论】:

  • 它工作得很好,但我想计算距离并基于纬度,因为我需要在 2 公里、2 英里内进行距离明智的搜索
  • 请检查有问题的更改
  • 我认为 database-api 选项需要深入了解 drupal 数据库结构,而 \Drupal::entityQuery 不需要深入了解结构以任何方式将距离查询集成到 \Drupal::entityQuery 方式中?
  • AFAIK 你不能像你希望的那样使用 entityQuery 的数据进行计算。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多