所以我对 MVC 世界还比较陌生。我现在正在做的是在 CakePHP2.0 中重写一个 php 项目。目前我正在处理订单,更具体地说是产品/库存的交付。
这是之前一个产品交付的sql代码:
$sqlSelect =("SELECT current_stock FROM stocks WHERE id IN (");
$sql = $con->prepare("UPDATE orders SET order_status=1 WHERE order_number=:orderNumber");
$sql2 = $con->prepare("UPDATE stocks SET current_stock= :stock + :delivered WHERE id = :stockID");
try {
$con->beginTransaction();
for($count=0; $count<=$_POST['count']; $count++) {
$idQuery [] ="?,";
$idArray []=($_POST['stock_id' .$count]);
}
$sqlSelect .= implode($idQuery);
$sqlSelect =trim($sqlSelect, ",") . ")"; // removes last comma from string, then closes the bracket //
$stmt =$con->prepare($sqlSelect);
$stmt->execute($idArray);
while ($row=$stmt->fetch()) {
$stock = ($row['current_stock']);
$sql2->bindParam(':stock', $stock);
}
for($count=0; $count<=$_POST['count']; $count++) {
$sql2->bindParam(':delivered', $_POST['delivery_amount' .$count]);
$sql2->bindParam(':stockID', $_POST['stock_id' .$count]);
$sql2->execute();
}
$sql->bindParam(':orderNumber', $_POST['order_num'] );
$sql->execute();
$con->commit();
$con=null;
echo "<script language=javascript>
alert('Delivery confirmed. Your stocks have been adjusted.')
location.replace('placeorder2.php')
</script>";
} catch (Exeception $e) {
$con->rollback();
echo "Update Failed: " . $e->getMessage();
}
?>
我想知道的是用“CakePHP”方式执行此操作的最简单方法是什么?
我的关联如下:产品有一个库存,产品有很多订单。