36

Codeigniter トランザクションを使用しています

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->trans_complete();

これは正常に動作します。私が抱えている問題は、内部でtrans_starttrans_completeの関数を呼び出しており、それらの関数がデータベースを処理しているため、挿入と更新といくつかの削除が含まれていることです...例:

$this->db->trans_start();
 $this->utils->insert_function($data);
 $this->utils->update_function2($test);
$this->db->trans_complete();

これらの関数が実行され、何らかのエラーが発生した場合、CodeIgniter はロールバックを行いません。

そのような問題に対処する最善の方法は何ですか?

私が念頭に置いている唯一の解決策は、これらの関数からエラーを返し、それらの関数内に ( trans_statand trans_complete) を追加することです。$this->db->trans_rollback

元:

    $this->db->trans_start();
     $result = $this->utils->insert_function($data);
     if($result === false){
       $this->db->trans_rollback();
     }
    $this->db->trans_complete();

これを行うより良い方法はありますか?

更新 1:

要求されたように、私が呼び出している外部関数のサンプル:

   // insert_function contains

    $rec = array(
        'numero' => $numero,
        'transaction_id' => $id,
        'debit' => $product_taxes['amount_without_taxes'],
        'date' => $data['date_transaction'],
    );
    $this->addExerciceAccountingRecords($rec);

  and addExerciceAccountingRecords contains

   function addExerciceAccountingRecords($records) {
    $this->db->insert('transactions_exercices', $records);
    }
4

6 に答える 6

4

この問題は、CodeIgniter がオブジェクトを処理する方法に関係していると思われます。


http://ellislab.com/codeigniter/user-guide/general/creating_libraries.htmlの「Creating Libraries」セクションにある CI ドキュメントにアクセスし、次
の関連セクションを参照してください。

$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

メイン コントローラーで、自動ロードを使用するか、クラスを明示的にロードすることにより、データベース クラスをロード/インスタンス化しました。

次に、先に進んでトランザクションを開き、utils ライブラリを介してデータベース関数にアクセスします。

ただし、$this-dbライブラリで使用すると、トランザクションに関連付けられたものではなく、データベース インスタンスの別のコピーに実際にアクセスすることになります。

同じインスタンスにアクセスするには、get_instance() 関数を使用する必要があります。

それはあなたの問題を解決するはずだと思います。機能をさまざまなモジュールに分割する独自のコーディング スタイルは優れています。この追加の詳細を理解する必要があります。

ロールバックが期待どおりに機能することを確認してください。

コードの根幹は、次のコントローラーで構成されています。

$this->db->trans_start();
$this->User_profile_m->create_new_user_profile();
$this->User_profile_m->create_new_user();
$this->db->trans_complete(); 

user_profile_mデータの永続性を処理するための単純なモデル:

function create_new_user()
{
    $data['user_name_usr'] = $this->input->post('user_name');
    $data['create_date_usr'] = NULL;

    $this->db->insert('user_usr', $data);  
}

function create_new_user_profile()
{
    $data['user_name_pro'] = $this->input->post('user_name');
    $data['user_description_pro'] = $this->input->post('user_description');
    $data['create_date_pro'] = NULL;

    $this->db->insert('user_profile_pro', $data);  
}

基本的に、デモでは 2 つの挿入 (2 つのテーブルのそれぞれに 1 つ) を試みます。一方の挿入が失敗すると、もう一方はロールバックされます。

私はこれを CodeIgniter 2.1.3 で構築しました。アプリケーション ファイルを GitHub から入手できるようにするか、圧縮して送信することができます。

于 2013-03-05T18:31:22.710 に答える
3


注:$this->db->trans_begin()手動トランザクションを実行するときは、必ず NOTを使用してください$this->db->trans_start()

$this -> db -> trans_begin(); 
$this -> utils -> insert_function ( $data );
$this -> utils -> update_function2 ( $test ); 
$this -> db -> trans_complete ();

MySql を使用する場合は認定し、InnoDb 形式で使用する

于 2014-01-30T12:04:26.630 に答える