gpt4 book ai didi

php - Cakephp 3 - 从数据库中减去一个值

转载 作者:行者123 更新时间:2023-11-29 19:02:18 25 4
gpt4 key购买 nike

嗨,我是 cakephp 的新手,并试图弄清楚它是如何工作的。

所以我有两个表,一个是客户付款,另一个是客户费用。当我向付款表添加值时,如何从费用中减去一个值?

例如 - 客户 X 的费用为 100。当我从付款中选择客户 X 并选择 20 的付款时。费用表中的答案应为 80。并且客户 X 也已付款,这应该记录在付款表中。

我正在提交表单中的值。并将其发送到支付 Controller 。

 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' );
}
}

如何执行相同操作并从费用表中减去该值?

在哪里可以进行计算以及如何进行?

这是我将值发送到 Controller 的表单。

<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>

表格 付款和收费都有一个 FK Customer_id。

最佳答案

您可以简单地添加一些逻辑:

在付款 Controller :

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
}
}

我只是假设您在费用表中也有“金额”字段!

关于php - Cakephp 3 - 从数据库中减去一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com