简体   繁体   English

交易后如何更新数据库?

[英]How to update the database after transaction?

I have a Drupal 7 site that has a customized theme installed on it and we have also added some new tables and extended some others. 我有一个Drupal 7网站,上面安装了自定义主题,我们还添加了一些新表并扩展了一些其他表。 Right now I have set up Ubercart for selling our products based on the taxonomy. 现在,我已经建立了Ubercart,以根据分类法销售我们的产品。 When the purchase is complete I need to update a custom table in MySQL so I made a proc to do that. 购买完成后,我需要在MySQL中更新自定义表,因此我进行了操作。

In MySQL the created a proc that will do the updating of the tables that I need, all I need to pass into the proc is the uid (same as from the users table) and an id of the taxonomy that was selected during purchase. 在MySQL中,创建了一个proc,它将对我需要的表进行更新,我需要传递给proc的就是uid(与用户表中的相同)和在购买过程中选择的分类法的id。

I have created the following code to make the call but I am not sure what the best way to pass in the uid and tid to the proc? 我创建了以下代码来进行调用,但是我不确定将uid和tid传递给proc的最佳方法是什么? Should I be using rules within Drupal? 我应该在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;
}
?>

You can fire your code after a purchase using Ubercart's hook_uc_checkout_complete() . 您可以在购买后使用Ubercart的hook_uc_checkout_complete()触发代码。 You will want to define that in a custom module, not from within the theme since this is business logic not display information. 您将需要在自定义模块中进行定义,而不是在主题内进行定义,因为这是不显示信息的业务逻辑。 From within the hook you will have access to both the order and the user account that placed the order. 在挂钩中,您将有权访问订单和下订单的用户帐户。

When working with Drupal, or any other framework really, you should leverage the built-in tools when you can to benefit from the structures they provide. 当与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)) {
  // ...
}

Code taken from: 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 取自以下代码: https//drupal.stackexchange.com/questions/32708/how-to-execute-stored-procedures-in-drupal ,依次引用: http : //public-action.org/content/stored-程序和drupal-7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM