如何在cakephp中使用事务

How to use Transactions in cakephp

我写了这段代码,但它不起作用,它给出了这个错误

Call to a member function commit() on a non-object

听说是我的代码

$datasource = $this->Arrear->getDataSource();               
$datasource->begin();
if($this->Customar->saveField("total_bake",$amount) && $this->Arrear->save()){

  $dataSource->commit();
  return  $this->redirect(array('action'=>'index'));

}else{

  $dataSource->rollback();
  $this->Session->setFlash('Data insert Failed','failure');

}

php 中的变量(因此在 cakephp 中也是如此)区分大小写 http://php.net/manual/en/language.variables.basics.php

你的第一行有

$datasource = $this->Arrear->getDataSource();

但你的承诺就像

$dataSource->commit();

您已将数据源分配给 $datasource,但没有分配给 $dataSource。最后一个变量甚至没有定义,这就是它显示该错误的原因。因此,您必须确保在所有地方都使用完全相同的变量(具有相同的大小写)。