我遇到了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;
}