Skip to content
Permalink
Browse files

added transaction helper

  • Loading branch information...
thinkingmedia
thinkingmedia committed Nov 18, 2016
1 parent 72d3d29 commit 078f6755455ebd8baaafaddd9cbc24ed5e9f05b9
Showing with 40 additions and 32 deletions.
  1. +40 −32 src/ORM/Table.php
@@ -36,7 +36,6 @@
use Cake\ORM\Exception\RolledbackTransactionException;
use Cake\ORM\Rule\IsUnique;
use Cake\Utility\Inflector;
use Cake\Validation\Validation;
use Cake\Validation\ValidatorAwareTrait;
use InvalidArgumentException;
use RuntimeException;
@@ -1435,6 +1434,36 @@ public function get($primaryKey, $options = [])
return $query->firstOrFail();
}
/**
* Handles the logic executing of a worker inside a transaction.
*
* @param bool $atomic Whether to execute the worker inside a database transaction.
* @param callable $worker The worker that will run inside the transaction.
* @return mixed
*/
protected function _executeTransaction($atomic, callable $worker)
{
if ($atomic) {
return $this->connection()->transactional(function () use ($worker) {
return call_user_func($worker);
});
}
return call_user_func($worker);
}
/**
* Checks if the caller would have executed a commit on a transaction.
*
* @param bool $atomic True if an atomic transaction was used.
* @param bool $primary True if a primary was used.
* @return bool Returns true if a transaction was committed.
*/
protected function _getCommitUsed($atomic, $primary)
{
return !$this->connection()->inTransaction() && ($atomic || (!$atomic && $primary));
}
/**
* Finds an existing record or creates a new one.
*
@@ -1475,13 +1504,9 @@ public function findOrCreate($search, callable $callback = null, $options = [])
'defaults' => true
];
if ($options['atomic']) {
return $this->connection()->transactional(function () use ($search, $callback, $options) {
return $this->_processFindOrCreate($search, $callback, $options);
});
}
return $this->_processFindOrCreate($search, $callback, $options);
return $this->_executeTransaction($options['atomic'], function () use ($search, $callback, $options) {
return $this->_processFindOrCreate($search, $callback, $options);
});
}
/**
@@ -1696,19 +1721,12 @@ public function save(EntityInterface $entity, $options = [])
return $entity;
}
$connection = $this->connection();
if ($options['atomic']) {
$success = $connection->transactional(function () use ($entity, $options) {
return $this->_processSave($entity, $options);
});
} else {
$success = $this->_processSave($entity, $options);
}
$success = $this->_executeTransaction($options['atomic'], function () use ($entity, $options) {
return $this->_processSave($entity, $options);
});
if ($success) {
if (!$connection->inTransaction() &&
($options['atomic'] || (!$options['atomic'] && $options['_primary']))
) {
if ($this->_getCommitUsed($options['atomic'], $options['_primary'])) {
$this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
}
if ($options['atomic'] || $options['_primary']) {
@@ -2033,21 +2051,11 @@ public function delete(EntityInterface $entity, $options = [])
'_primary' => true,
]);
$process = function () use ($entity, $options) {
$success = $this->_executeTransaction($options['atomic'], function () use ($entity, $options) {
return $this->_processDelete($entity, $options);
};
$connection = $this->connection();
if ($options['atomic']) {
$success = $connection->transactional($process);
} else {
$success = $process();
}
});
if ($success &&
!$connection->inTransaction() &&
($options['atomic'] || (!$options['atomic'] && $options['_primary']))
) {
if ($success && $this->_getCommitUsed($options['atomic'], $options['_primary'])) {
$this->dispatchEvent('Model.afterDeleteCommit', [
'entity' => $entity,
'options' => $options

0 comments on commit 078f675

Please sign in to comment.
You can’t perform that action at this time.