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

Cakephp 3 - Subtracting a value from the database

您好,我是 cakephp 的新手,正在尝试弄清楚它是如何工作的。

所以我有两个 table,一个是 Customer Payments,另一个是 CustomerCharges。当我向 Payments table 添加一个值时,如何从 Charges 中减去一个值?

例如 - 客户 X 的费用为 100。当我 select 客户 X 付款且 select 付款为 20。Charges 中的答案应为 80 table。它应该记录在 Payments table 上,表明客户 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' );
         }
 }

我如何做同样的事情并从 费用 table 中减去该值?

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

这是我向控制器发送值的表单。

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

这样做

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

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

在支付控制器:

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

收费table:

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

而且我假设您在费用 table 中也有 'amount' 字段!