0

我在使用 cakephp 时遇到问题。我有一个包含借方和贷方列的数据库表。下面,我尝试从第一个用户账户中扣款并保存记录。如果保存成功,它会更改相同的数组并将所有者 ID 更改为将要汇款的用户的 ID,并将借方记为贷方。问题是这个

不正确

user_id payment_to_id credit debit
1       2             0.00   3.00
1       2             3.00   0.00

下面是它应该如何显示。由于 user_id 2 正在收款,payment_to_id 为空。但它不是这样工作的。它的工作方式与上面显示的一样,这是不正确的。我正在使用 create() 方法创建新记录,但它不起作用。我究竟做错了什么

user_id payment_to_id credit debit
1       2             0.00   3.00
2       null          3.00   0.00

下面是代码

    function advanced_transfer($data = array()) {
        if(!empty($data)) {
            //I store the original credit in a variable called credit
            $credit = $data['PaymentTransaction']['credit'];
            //since I have credit stored, a zero it out so it wont save
            $data['PaymentTransaction']['credit'] = 0;
            //then I first set up a debit transaction
            $data['PaymentTransaction']['debit'] = $credit;
            //I am storing both the account owner and payment to id's
            $accountOwnerId = $data['PaymentTransaction']['user_id'];
            $paymentToId = $data['PaymentTransaction']['payment_to_id'];
            $this->create();//new record
            if($this->save($data, false)) {//save
                $data['PaymentTransaction']['user_id'] = $paymentToId;//I set the owner to the payment to user
                $data['PaymentTransaction']['payment_to_id'] = null;//I null out payment to cause the user is recieving
                $data['PaymentTransaction']['credit'] = $credit;//I set theirs to credit
                $data['PaymentTransaction']['debit'] = 0;//and null out debit cause we arent taking out
                $this->create();//create new record
                if(!$this->save($data, false)) {//save with no validation again
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }

        return true;
    }
4

0 回答 0