dsklzerpx64815631 2014-06-16 06:56 采纳率: 100%
浏览 15
已采纳

如何从数据库中删除数据?

I have two table, BrokerInfo and BrokerBank in my database. If data is saved in BrokerInfo then it will start the process to save data in BrokerBank table. If it is failed to save data in table 2 then it should remove data from table 1 in database. I am facing problem to remove data from table1 in database, how can i do it? Here is my code for add action :

public function add() {
    if ($this->request->is('post'))
    {   

        $this->BrokerInfo->create();

            $this->BrokerInfo->save($this->request->data);
            $id=$this->BrokerInfo->getLastInsertId();
            $this->BrokerBank->begin();
            $this->BrokerBank->create();

            $this->request->data['BrokerBank']['broker_info_id'] = $id;

            if($this->BrokerBank->save($this->request->data))
            {   

                $this->BrokerInfo->commit();
                $this->redirect(array('action' => 'index'));
            }
            else 
            {   
                $this->BrokerInfo->rollback();              
                $this->Session->setFlash(__('The information could not be saved. Please, try again.'));
            }
    }
}
  • 写回答

1条回答 默认 最新

  • druybew06513 2014-06-16 07:03
    关注

    Start the transaction

    $this->BrokerBank->begin();
    

    before you do anything. If the save fails, everything gets undone automatically. Currently you do the save for table1 before you start the transaction. And this means it does not get reverted in the rollback.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程