如何在cakephp中使用事务

时间:2015-07-05 09:00:49

标签: mysql cakephp transactions

我编写了这段代码,但它没有用,它给出了这个错误

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');

}

1 个答案:

答案 0 :(得分:0)

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

你有第一行

SELECT 
    Table1.ID,

    -- CASE WHEN Table1.Code1 = t1.Code1 THEN NULL ELSE Table1.Code1 END AS Code1,
    CASE WHEN LAG(Table1.Code1) OVER (ORDER BY Table1.ID) = Table1.Code1 THEN NULL ELSE Table1.Code1 END AS Code1,

    Table1.Code2, Table1.Details, Table1.IDS, 

    -- CASE WHEN Table1.Name = t1.Name THEN NULL ELSE Table1.Name END AS Name,
    CASE WHEN LAG(Table2.Name) OVER (ORDER BY Table1.ID) = Table2.Name THEN NULL ELSE Table2.Name END AS Name

FROM 
    Table1 
INNER JOIN 
    Table2 ON Table1.Code1 = Table2.Code1 
-- LEFT JOIN Table1 t1 ON Table1.ID = t1.ID + 1 
WHERE 
    Table1.IDS = 1 
ORDER BY 
    Table1.Code1, Table1.Code2

但你承诺

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

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