Skip to content
Permalink
Browse files

Making link() transational too

  • Loading branch information...
lorenzo committed Dec 15, 2013
1 parent fd5dd65 commit 53e6d87df4857ecb32e2df4c8ed0af2ab0eabace
Showing with 20 additions and 6 deletions.
  1. +14 −5 Cake/ORM/Association/BelongsToMany.php
  2. +6 −1 Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
@@ -421,7 +421,12 @@ public function link(Entity $sourceEntity, array $targetEntities, array $options
$links = $sourceEntity->get($property) ?: [];
$links = array_merge($links, $targetEntities);
$sourceEntity->set($property, $links);
return $this->_saveLinks($sourceEntity, $targetEntities, $options);
return $this->junction()->connection()->transactional(
function() use ($sourceEntity, $targetEntities, $options) {
return $this->_saveLinks($sourceEntity, $targetEntities, $options);
}
);
}
/**
@@ -483,7 +488,7 @@ public function replaceLinks(Entity $sourceEntity, array $targetEntities, array
throw new \InvalidArgumentException;
}
$this->junction()->connection()->transactional(
return $this->junction()->connection()->transactional(
function() use ($sourceEntity, $targetEntities, $primaryValue, $options) {
$foreignKey = (array)$this->foreignKey();
$existing = $this->_junctionTable->find('all')
@@ -495,10 +500,14 @@ function() use ($sourceEntity, $targetEntities, $primaryValue, $options) {
$property = $this->property();
$sourceEntity->set($property, $inserts);
if ($this->save($sourceEntity, $options + ['associated' => false])) {
$sourceEntity->set($property, $targetEntities);
$sourceEntity->dirty($property, false);
if ($inserts && !$this->save($sourceEntity, $options + ['associated' => false])) {
return false;
}
$sourceEntity->set($property, $targetEntities);
$sourceEntity->dirty($property, false);
return true;
}
);
}
@@ -683,7 +683,12 @@ public function testLinkWithNotPersistedTarget() {
* @return void
*/
public function testLinkSuccess() {
$joint = $this->getMock('\Cake\ORM\Table', ['save'], [['alias' => 'ArticlesTags']]);
$connection = \Cake\Database\ConnectionManager::get('test');
$joint = $this->getMock(
'\Cake\ORM\Table',
['save'],
[['alias' => 'ArticlesTags', 'connection' => $connection]]
);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,

0 comments on commit 53e6d87

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