Skip to content

Commit

Permalink
wrap findOrCreate with transactional
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingmedia committed Jun 20, 2016
1 parent 5add51d commit a0b1201
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/ORM/Table.php
Expand Up @@ -1222,17 +1222,19 @@ public function get($primaryKey, $options = [])
*/
public function findOrCreate($search, callable $callback = null)
{
$query = $this->find()->where($search);
$row = $query->first();
if ($row) {
return $row;
}
$entity = $this->newEntity();
$entity->set($search, ['guard' => false]);
if ($callback) {
$callback($entity);
}
return $this->save($entity) ?: $entity;
$this->connection()->transactional(function() use($search, $callback) {
$query = $this->find()->where($search);
$row = $query->first();
if ($row) {
return $row;
}
$entity = $this->newEntity();
$entity->set($search, ['guard' => false]);
if ($callback) {
$callback($entity);
}
return $this->save($entity) ?: $entity;
});
}

/**
Expand Down

0 comments on commit a0b1201

Please sign in to comment.