CakePHP 2.3.x database transaction

后端 未结 1 407
猫巷女王i
猫巷女王i 2021-02-15 00:15

I need your help using transactions in CakePHP.

I have a Product model, with clause hasMany to Price and Property models (key product_id).

In my Product model,

1条回答
  •  时光说笑
    2021-02-15 01:15

    Tables must be in InnoDb format. MyISAM format of tables doesn't support transactions.

    No need to insert additional code into model.

    ProductController:

    $datasource = $this->Product->getDataSource();
    try {
        $datasource->begin();
        if(!$this->Product->save($data)
            throw new Exception();
    
        if(!$this->Price->save($data_one)
            throw new Exception();
    
        if(!$this->Property->save($my_data)
            throw new Exception();
    
        $datasource->commit();
    } catch(Exception $e) {
        $datasource->rollback();
    }
    

    0 讨论(0)
提交回复
热议问题