我有一个tansaction
表,并有一个名为balance
的字段。在这里,我尝试使用新数据更新此字段,但在新事务中插入。所以我需要控制器中的平衡场。所以,我试过下面的代码。
$client_id = $this->request->data['Transaction']['user_id'];
$new = $this->Transaction->find('first', array(
'conditions'=>array('Transaction.user_id'=> $client_id),
'fields'=>array('Transaction.balance')
));
$this->request->data['Transaction']['balance'] =$this->request->data['Transaction']['debit_money']-$this->request->data['Transaction']['credit_money']+new;
但在这里,我没有得到旧的余额数据。
答案 0 :(得分:1)
$new
是一个关联数组。您正在为数字添加数组。
尝试以下方法:
$client_id = $this->request->data['Transaction']['user_id'];
$new = $this->Transaction->find('first', array(
'conditions'=>array('Transaction.user_id'=> $client_id),
'fields'=>array('Transaction.balance')
));
$this->request->data['Transaction']['balance'] =
$this->request->data['Transaction']['debit_money']
- $this->request->data['Transaction']['credit_money']
+ $new['Transaction']['balance'];
或者,这也应该有效:
$client_id = $this->request->data['Transaction']['user_id'];
$this->request->data['Transaction']['balance'] =
$this->request->data['Transaction']['debit_money']
- $this->request->data['Transaction']['credit_money']
+ $this->Transaction->field('balance',array('Transaction.user_id'=> $client_id),
));