Drupal SELECT查询,SQL语法中发生意外错误

问题描述 投票:0回答:1

在我的自定义模块中,我试图基于两个参数,foodType(可以是冰或华夫饼干)和限制来从数据库中选择订单。

到目前为止,函数和查询如下所示:

public static function getOrders($foodType, $limit){
    $connection = Database::getConnection();
    $query = $connection->select('ice_cream', 'orders');
    $query->condition('orders.foodType', $foodType, '=')
      ->orderBy('id','DESC')
      ->range(0, $limit);
    return $query->execute()->fetchAll();
  }

表的结构如下:ice cream table structure

错误报告:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM ice_cream ice_cream' at line 1: SELECT FROM {ice_cream} ice_cream; Array ( ) in Drupal\ice_cream\Controller\OrderController::getOrders() (line 57 of C:\xampp\htdocs\imd-theming\site\modules\custom\ice_cream\src\Controller\OrderController.php).

页面错误:

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Drupal\Core\Database\DatabaseExceptionWrapper</em>: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#039;FROM
ice_cream orders
WHERE orders.foodType = &#039;ice&#039;
ORDER BY id DESC
LIMIT 5 OFF&#039; at line 1: SELECT 
FROM
{ice_cream} orders
WHERE orders.foodType = :db_condition_placeholder_0
ORDER BY id DESC
LIMIT 5 OFFSET 0; Array
(
    [:db_condition_placeholder_0] =&gt; ice
)
 in <em class="placeholder">Drupal\ice_cream\Controller\OrderController::getOrders()</em> (line <em class="placeholder">54</em> of <em class="placeholder">modules\custom\ice_cream\src\Controller\OrderController.php</em>). <pre class="backtrace">Drupal\Core\Database\Statement-&gt;execute(Array, Array) (Line: 631)
Drupal\Core\Database\Connection-&gt;query(&#039;SELECT 
FROM
{ice_cream} orders
WHERE orders.foodType = :db_condition_placeholder_0
ORDER BY id DESC
LIMIT 5 OFFSET 0&#039;, Array, Array) (Line: 358)
Drupal\Core\Database\Driver\mysql\Connection-&gt;query(&#039;SELECT 
FROM
{ice_cream} orders
WHERE orders.foodType = :db_condition_placeholder_0
ORDER BY id DESC
LIMIT 5 OFFSET 0&#039;, Array, Array) (Line: 510)
Drupal\Core\Database\Query\Select-&gt;execute() (Line: 54)
Drupal\ice_cream\Controller\OrderController::getOrders(&#039;ice&#039;, &#039;5&#039;) (Line: 88)
... (Omitted the rest since it's not SQL related anymore.)
drupal drupal-8
1个回答
0
投票

我认为您需要告诉它哪些领域可以撤回。例如,如果您想为每条记录提取ID,则可以执行以下操作:

$query->fields('orders', ['id']);
© www.soinside.com 2019 - 2024. All rights reserved.