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

时间:2017-05-04 04:31:09

标签: php mysql cakephp cakephp-2.0 cakephp-3.0

嗨,我是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>

表  付款与支付费用都具有Customer_id的FK。

2 个答案:

答案 0 :(得分:0)

这样做

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

答案 1 :(得分:0)

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

付款控制人员

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

而我只是假设你有数量&#39;费用表中的字段也是如此!