2013-05-03 49 views
0

我有2个模型:modelA,modelB。我想在myController的CI事务中实现与这些模型相关的2个操作。例如:关于CodeIgniter中事务的问题

$this->db->trans_start(); 

$this->modelA->uodateA(conditions, item); 
$this->modelB->updateB(conditions, item); 
// with conditions and item is valid 
$this->db->trans_complete(); 

if ($this->db->trans_status() === FALSE) 
{ 
//handle when failed 
} 

这里是这些模式2操作:)

public function updateA($where = array(), $item = array(), $auth = NULL){ 
     if(!is_array($item) && empty($item)) return FALSE; 

     if(is_numeric($where)){ 
      $where = array('vote_id'=>$where); 
     } 
     $this->db->where($where)->update($this->_table,$item); 


     return ($this->db->affected_rows() > 0); 
    } 


public function updateB($where = array(), $item = array(), $auth = NULL){ 
     if(!is_array($item) && empty($item)) return FALSE; 

      if(is_numeric($where)){ 
       $where = array('vote_id'=>$where); 
      } 

     $this->db->where($where)->update($this->_table,$item); 
     return ($this->db->affected_rows() > 0); 
    } 

虽然updateB(失败(例如:不能有要更新的记录,用字符串值更新的int字段... )但trans_status()仍然返回true。我想当updateB()失败时它必须回滚。怎么修?非常感谢

+0

你可以发布你的模型代码吗? – 2013-05-03 05:03:58

+0

@Kishor:我更新了 – secretlm 2013-05-03 07:38:44

+0

只要您的模型方法中没有其他交易语句,您的示例代码应该可以正常工作。 – 2013-05-03 09:43:01

回答

0

你可以试试这个

$this->db->trans_begin(); 

$this->modelA->insertA(); 
$this->modelB->updateB(); 

if ($this->db->trans_status() === FALSE) 
{ 
    $this->db->trans_rollback(); 
} 
else 
{ 
    $this->db->trans_commit(); 
} 
+0

我尝试过,但仍然失败。 – secretlm 2013-05-03 06:29:55

+0

你应该在一个模型中写上面的代码。你可以发布你的两个查询吗?它会帮助我们纠正。 – Sudz 2013-05-03 06:45:50

+0

我更新了我的代码 – secretlm 2013-05-03 07:56:38