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

标签 php mysql cakephp cakephp-2.0 cakephp-3.0

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

相关文章:

mySQL 返回文本字段中的单词列表

CakePHP 2.0+ 在线共享主机设置

php - CakePHP,来自模型的查询

php - 使用枢轴表进行 Eloquent 或查询构建器搜索

php - 根据用户输入将排名添加到 mysql 数据库

php - 设置根据 MySQL 值选择的下拉列表

php - 将动态输入字段插入数据库

php - 将结果合并到一个展开的表行中

authentication - CakePHP无需登录即可访问页面

php - 使用 openssl : recompile with -fPIC 编译 PHP 时出错