http://datamapper.wanwizard.eu/pages/transactions.html
DataMapper handles transactions in very much the same way that
CodeIgniter does (read CodeIgniter Transactions), obviously because it
uses the same methods! The only real difference is that you'll be
calling the transaction methods directly on your DataMapper objects.
This means that instead of:
$this->db->trans_begin();
You'd use:
$my_datamapper_object->trans_begin();
Everything else, according to the Datamapper documentation, is identical to Codeigniter in regards to transactions. If you look at the source code for the Datamapper library, you'll see that all trans_*()
calls are simply wrapper functions. Example:
// Datamapper.php 1.8.dev line 3975
/**
* Trans Complete
*
* Complete a transaction.
*
* @return bool Success or Failure
*/
public function trans_complete()
{
return $this->db->trans_complete();
}
A trans_end()
method does not exist in either Codeigniter or Datamapper. You'd use trans_start()
and trans_complete()
for automatic transactions, or to call them manually:
http://codeigniter.com/user_guide/database/transactions.html
Running Transactions Manually
If you would like to run transactions manually you can do so as
follows:
$this->db->trans_begin();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
Just replace $this->db
with your Datamapper object. For example.
$u = new User($id);
$u->trans_begin();
$u->name = 'Jorge';
$u->save();
if ($u->trans_status() === FALSE)
{
$u->trans_rollback();
}
else
{
$u->trans_commit();
}