Skip to content

Instantly share code, notes, and snippets.

@stephenmelrose
Created February 11, 2015 11:53
Show Gist options
  • Save stephenmelrose/c0195f5b3bd2315ab6b5 to your computer and use it in GitHub Desktop.
Save stephenmelrose/c0195f5b3bd2315ab6b5 to your computer and use it in GitHub Desktop.
Lazy Transactions Example
<?php
public function controllerAction($param1, $param2) {
$transactionStarted = false;
try {
$data = $this->modelCall($param1, $param2, $transactionStarted);
if($transactionStarted) {
$this->db->commit();
}
}
catch(\Exception $e) {
if($transactionStarted) {
$this->db->rollBack();
}
}
return $data;
}
public function modelCall($param1, $param2, &$transactionStarted) {
$data = $this->repo->getSomeData();
if($data == $somethingThatRequiresAChange) {
if(!$transactionStarted) {
$this->db->beginTransaction();
$transactionStarted = true;
}
$this->doSomeWriteThing();
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment