Skip to content
Permalink
Browse files

Re-wrapped _processFindOrCreate in transaction, manually dispatched a…

…fterSaveCommit
  • Loading branch information...
jeremyharris committed May 1, 2018
1 parent dd8b5ab commit 58bb5f459f26c8e94d6534f6e7ef72e3efa8bf45
Showing with 13 additions and 2 deletions.
  1. +11 −2 src/ORM/Table.php
  2. +2 −0 tests/TestCase/ORM/TableTest.php
@@ -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;
}
/**
@@ -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);

0 comments on commit 58bb5f4

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