7

is it possible (how) to use mysql transactions and rollbacks using kohana ORM ?

gpilotino
  • 13,055
  • 9
  • 48
  • 61

3 Answers3

8

SQL Transactions in Kohana 3.x are not done the same way as in 2.x. In 3.x, the database class comes with transaction methods:

$db->begin();
$db->commit();
$db->rollback();

This also works if you are using ORM stuff. Just initiate the transaction before ORM saves, updates, a or deletes.

Read more in this post: http://dev.strategystar.net/2011/10/sql-transactions-with-kohana-3-x/

In 2.x, transactions had to be done manually:

$this->db->query("START TRANSACTION")
JV-
  • 406
  • 1
  • 4
  • 11
7

Check out the official forums. It shows an example on how to use transactions with Ko3:

$db->query(NULL, 'TRANSACTION START');  
// Do stuff  
$db->query(NULL, 'COMMIT');

How to do it with version 2, I don't know. I'm still new to kohana myself, and learning Ko3 rather than 2. But I'm guessing it's quite similar.

SpadXIII
  • 906
  • 4
  • 5
3

I've created a Kohana module that makes using transactions a lot easier:

https://github.com/brazzy/kohana-transactional

It does, however, require at least Kohana 3.1. But then you just add

public $_transactional = true;

to the controller, and all actions are automatically executed inside a transaction, which is rolled back when the action fails with an exception.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720