交易后如何更新数据库?

How to update the database after transaction?

我有一个安装了自定义主题的 Drupal 7 站点,我们还添加了一些新的 tables 并扩展了一些其他主题。 现在我已经设置了 Ubercart 来根据分类法销售我们的产品。购买完成后,我需要在 MySQL 中更新自定义 table,所以我制作了一个 proc 来执行此操作。

在 MySQL 中创建了一个 proc 来更新我需要的 tables,我需要传递给 proc 的只是 uid(与来自用户 table) 和在购买过程中选择的分类法的 id。

我创建了以下代码来进行调用,但我不确定将 uid 和 tid 传递给 proc 的最佳方式是什么?我应该在 Drupal 中使用规则吗?

<?php
$mysqli = new mysqli("mysite.com", "user", "password", "db1");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("CALL UpdateUserDestList(uID, tID")) {
    echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>

您可以在使用 Ubercart 的 hook_uc_checkout_complete() 购买后触发您的代码。您将希望在自定义模块中而不是在主题中定义它,因为这是不显示信息的业务逻辑。从挂钩内,您将可以访问订单和下订单的用户帐户。

在使用 Drupal 或任何其他框架时,您应该尽可能利用内置工具以从它们提供的结构中获益。

// Get the Drupal database connection and change the statement class to PDOStatement.
// Save the current class for cleanup later.
$conn = Database::getConnection();
$saved_class = $conn->getAttribute(PDO::ATTR_STATEMENT_CLASS);
$conn->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatement'));

// Prepare the statement and bind params
$statement = $conn->prepare("Call GetNodeList(?,?)");

$op_status = $statement->bindParam(1, $node_type, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 25);
$op_status = $statement->bindParam(2, $publish_state, PDO::PARAM_INT | PDO::PARAM_INPUT_OUTPUT);

// Execute the statement and reset the connection's statement class to the original.
$exec_result = $statement->execute();
$conn->setAttribute(PDO::ATTR_STATEMENT_CLASS, $saved_class);

// Get your data
while ($row = $statement->fetchColumn(0)) {
  // ...
}

代码取自:https://drupal.stackexchange.com/questions/32708/how-to-execute-stored-procedures-in-drupal which in turn cites: http://public-action.org/content/stored-procedures-and-drupal-7