So I am relatively new to the MVC world. What I am doing at the moment is rewriting a php project in CakePHP2.0. Currently I am working on orders, more specifically the delivery of products/stock.
This is the previous sql code for a delivery of products:
$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();
}
?>
What I would like to know is what is the easiest way to perform this the 'CakePHP' way?
My Associations are as follows: Product hasOne Stock, Product hasMany Order.