所以我对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”方式的最简单方法是什么?
我的协会如下:产品有一个库存,产品有多个订单。
答案 0 :(得分:1)
是的,我设法让它发挥作用。
所以在我的Order Model中,我创建了一个方法来循环遍历从OrdersController检索到的$ this-&gt;数据哈希并应用我必要的逻辑,并将重建的哈希返回给Controller。
class Order extends Model {
...
public function finalizeOrder ($dataHash= array())
{
$stockHash = array();
foreach ($dataHash['Stock'] as $key => $value) {
$stockHash['Stock'][$key]['id'] = $value['id'];
$stockHash['Stock'][$key]['current_stock'] = $value['current_stock'] + $value['delivered_units'];
}
foreach ($dataHash['Order'] as $key => $value) {
$stockHash['Order'][$key]['delivered'] = $value['delivered'];
$stockHash['Order'][$key]['id'] = $value['id'];
}
return $stockHash;
}
}
我的控制器看起来像这样:
class OrdersController extends AppController {
...
public function acceptDelivery() {
$this->loadModel('Stock');
$this->data = $this->Order->finalizeOrder($this->data);
if ($this->Stock->saveAll($this->data['Stock']))
{
if ($this->Order->saveAll($this->data['Order'])) {
$this->Session->setFlash('Delivery has been confirm. Your stock has been updated.');
$this->redirect(array('action' => 'index'));
}
} else {
$this->Session->setFlash('There was an error');
$this->redirect(array('action' => 'index'));
}
}
}
我的表单(我省略了一些代码):
<? echo $this->Form->create('Order', array('action' => 'acceptDelivery')); ?>
<table class="gnr">
<tr>
<th class="product">
Product Name
</th>
<th class="product" style="width: 5%">
Qty
</th>
<th class="product" style="width: 5%">
Received
</th>
</tr>
<? foreach ($order as $key => $value): ?>
<tr>
<td class="product">
<? echo $value['Product']['product_name']; ?>
</td>
<td class="product">
<? echo $value['Order']['order_quantity'];?>
</td>
<td class="product">
<? echo $this->Form->number('Stock.'.$key.'.delivered_units', array(
'min' => '0', 'class' => 'val1', 'value' => 0
)); ?>
</td>
</tr>
<? echo $this->Form->hidden('Stock.'.$key.'.id', array(
'value' => $value['Stock']['id'] )); ?>
<? echo $this->Form->hidden('Order.'.$key.'.delivered', array(
'value' => 1 )); ?>
<? echo $this->Form->hidden('Order.'.$key.'.id', array(
'value' => $value['Order']['id'] )); ?>
<? echo $this->Form->hidden('Stock.'.$key.'.current_stock', array(
'value' => $value['Stock']['current_stock'] )); ?>
<? endforeach; ?>
</table>
<? echo $this->Form->end('Confirm Order'); ?>