Skip to content

Commit

Permalink
Re-wrapped _processFindOrCreate in transaction, manually dispatched a…
Browse files Browse the repository at this point in the history
…fterSaveCommit
  • Loading branch information
jeremyharris committed May 1, 2018
1 parent dd8b5ab commit 58bb5f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/ORM/Table.php
Expand Up @@ -1677,10 +1677,19 @@ public function findOrCreate($search, callable $callback = null, $options = [])
{
$options += [
'atomic' => true,
'defaults' => true
'defaults' => true,
'_primary' => true,
];

return $this->_processFindOrCreate($search, $callback, $options);
$entity = $this->_executeTransaction(function () use ($search, $callback, $options) {
return $this->_processFindOrCreate($search, $callback, $options);
}, $options['atomic']);

if ($entity && $this->_transactionCommitted($options['atomic'], $options['_primary'])) {
$this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
}

return $entity;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -6157,9 +6157,11 @@ public function testFindOrCreateTransactions()
$article = $articles->findOrCreate(function ($query) {
$this->assertInstanceOf('Cake\ORM\Query', $query);
$query->where(['title' => 'Find Something New']);
$this->assertTrue($this->connection->inTransaction());
}, function ($article) {
$this->assertInstanceOf('Cake\Datasource\EntityInterface', $article);
$article->title = 'Success';
$this->assertTrue($this->connection->inTransaction());
});
$this->assertFalse($article->isNew());
$this->assertNotNull($article->id);
Expand Down

0 comments on commit 58bb5f4

Please sign in to comment.