在我的Drupal 8站点中,我有一个“搜索”页面功能。为此,我正在尝试将我的自定义SQL查询与核心Drupal搜索查询连接起来。我正在尝试向.module文件中的SQL查询添加参数,但它向我抛出了语法错误。我的查询如下:function mymodule_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query) {//If my view is my-search and the Miles field is not empty and not 'Any' and zipcode field is not empty $searched_miles_value = $view->getRequest()->get('field_miles_value'); $searched_zip_value = $view->getRequest()->get('field_zip_value'); if(!empty($searched_miles_value)) { _remove_view_query_condition($query, 'node__field_miles.field_miles_value'); } if(!empty($searched_zip_value)) { _remove_view_query_condition($query, 'node__field_zip.field_zip_value'); } if($view->id() == 'my_search' && !empty($searched_miles_value) && !empty($searched_zip_value) && ($searched_miles_value != 'any')) { $connection = \Drupal\Core\Database\Database::getConnection(); $result = $connection->query('SELECT to_zip FROM zipmaster_xref WHERE from_zip = [field_zip_value] AND miles = [field_miles_value]')->fetchAll(); $target_zips = ['98567'];//hardcoded for testing foreach($result as $zip) { $target_zips[] = $zip->to_zip; } $query->addWhere('new_group', 'node__field_zip.field_zip_value', $target_zips, 'IN'); }我在SQL查询行上遇到语法错误。有什么帮助吗?
1 回答
慕侠2389804
TA贡献1719条经验 获得超6个赞
尝试在查询中使用变量:
$result = $connection->query("SELECT to_zip FROM zipmaster_xref WHERE from_zip = '".$searched_zip_value."' AND miles = '".$searched_miles_value."'")->fetchAll();
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报
0/150
提交
取消