在我的自定义模块中,我试图基于两个参数,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 'FROM
ice_cream orders
WHERE orders.foodType = 'ice'
ORDER BY id DESC
LIMIT 5 OFF' 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] => 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->execute(Array, Array) (Line: 631)
Drupal\Core\Database\Connection->query('SELECT
FROM
{ice_cream} orders
WHERE orders.foodType = :db_condition_placeholder_0
ORDER BY id DESC
LIMIT 5 OFFSET 0', Array, Array) (Line: 358)
Drupal\Core\Database\Driver\mysql\Connection->query('SELECT
FROM
{ice_cream} orders
WHERE orders.foodType = :db_condition_placeholder_0
ORDER BY id DESC
LIMIT 5 OFFSET 0', Array, Array) (Line: 510)
Drupal\Core\Database\Query\Select->execute() (Line: 54)
Drupal\ice_cream\Controller\OrderController::getOrders('ice', '5') (Line: 88)
... (Omitted the rest since it's not SQL related anymore.)
我认为您需要告诉它哪些领域可以撤回。例如,如果您想为每条记录提取ID,则可以执行以下操作:
$query->fields('orders', ['id']);