嗨,我是新来的cakephp,试图弄清楚事情是如何运作的。
我有两张桌子一张是客户付款
客户X的费用是100。当我从付款中选择客户X并选择20的付款时。收费表中的答案应该是80。它应该记录在付款表上,客户X也已经付款了。
我正在提交表单中的值。并将其发送给支付控制器。
public function addPaymentsCustomer()
{
$this->viewBuilder()->layout(false);
$payment = $this->Payments->newEntity();
if ($this->request->is('post')) {
$payment = $this->Payments->patchEntity($payment, $this->request->data);
if ($this->Payments->save($payment)) {
$this->Flash->success(__('The payment has been saved.'));
return $this->redirect( '/payments' );
} else {
$this->Flash->error(__('The payment could not be saved. Please, try again.'));
return $this->redirect( '/payments' );
}
$this->Flash->error(__('Error.. Please, try again.'));
return $this->redirect( '/payments' );
}
}
我如何做同样的事情并从费用表中减去该值?
我可以在哪里进行计算以及如何进行计算?
这是我向控制器发送值的表单。
<form role="form" method="post" accept-charset="utf-8" action="/add-payments-customer">
<div style="display:none;"><input type="hidden" name="_method" value="POST"/></div>
<br style="clear:both">
<div class="form-group">
<div class="input-group col-md-5 col-lg-5 col-sm-5 col-xs-12">
<select class="form-control" style="background-color: #F9F9F9;" name="customer_id" id="customer_id" onchange="this.form.submit()">
<option style="padding-top: 20px;">Select a Customer</option>
<?php
if (!empty($CustomerInfo)) {
foreach ($CustomerInfo as $Customers):
?>
<option value="<?= h($Customers->id)?>"><?= h($Customers->name)?></option>
<?php endforeach; } ?>
</select>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control secondlead" id="description" name="description" placeholder="Description" required>
</div>
<div class="form-group">
<input type="number" id="amount" name="amount" class="form-control secondlead" placeholder="Amount">
</div>
<div class="pull-left">
<button type="submit" id="submit" name="submit" class="btn btn-primary greenbtn">Send</button>
</div>
<div style="margin-top: 10px" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p class="text-center"><?= $this->Flash->render() ?></p>
</div>
</form>
表格付款
您可以简单地添加一些逻辑:
付款控制员:
public function addPaymentsCustomer()
{
$this->viewBuilder()->layout(false);
$payment = $this->Payments->newEntity();
if ($this->request->is('post')) {
$payment = $this->Payments->patchEntity($payment, $this->request->data);
$this->loadModel('Charges');
$data['amount'] = $this->request->data['amount'];
$data['customer_id'] = $this->request->data['customer_id'];
if ($this->Payments->save($payment)) {
$this->Flash->success(__('The payment has been saved.'));
$this->Charges->updateCharge($data); // update charges
return $this->redirect( '/payments' );
} else {
$this->Flash->error(__('The payment could not be saved. Please, try again.'));
return $this->redirect( '/payments' );
}
$this->Flash->error(__('Error.. Please, try again.'));
return $this->redirect( '/payments' );
}
}
在收费表:
public function updateCharge($data = []) {
$customer_id = $data['customer_id'];
$charge = $this->find()
->where(['Charges.customer_id' => $customer_id])
->first();
$previousAmount = $charge->amount;
$newAmount = $previousAmount - $data['amount'];
$charge->customer_id = $customer_id;
$charge->amount = $newAmount;
if($this->save($charge)) {
// updated
}
}
我只是假设你在收费表中也有“金额”字段!
你喜欢这样吗
use Cake\Datasource\ConnectionManager;// At the top of file
$conn = ConnectionManager::get('default');
$conn->begin(); // Start transaction
if ($this->Payments->save($payment)) {
$this->loadModel('Customers');
$customer = $this->Customers->get($payment->customer_id);
$customer->amount = $customer->amount + $payment->amount;
if($this->Customers->save($customer)){
$conn->commit(); // Commit transaction
$this->Flash->success(__('The payment has been saved.'));
} else {
$conn->rollback();// Rollback transaction
$this->Flash->error(__(json_encode($customer->errors()))); // Shows what get wrong
}
return $this->redirect('/payments');
} else {
$conn->rollback();// Rollback transaction
$this->Flash->error(__(json_encode($payment->errors()))); // Shows what get wrong
return $this->redirect('/payments');
}