Skip to content

Commit

Permalink
Renaming Association::save() to Association::saveAssociated()
Browse files Browse the repository at this point in the history
Given that Association is also a proxy for any Table method, having
function that does a different thing to another with the same name in
Table was not a good idea. This removes the name clash and allow the
original save() to be proxied correctly
  • Loading branch information
lorenzo committed Jun 6, 2014
1 parent 92754d0 commit 3df2a07
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 45 deletions.
5 changes: 3 additions & 2 deletions src/ORM/Association.php
Expand Up @@ -763,14 +763,15 @@ public abstract function cascadeDelete(Entity $entity, array $options = []);
public abstract function isOwningSide(Table $side);

/**
* Proxies the saving operation for an entity to the target table
* Extract the target's associaiton data our from the passed entity and proxies
* the saving operation to the target table.
*
* @param \Cake\ORM\Entity $entity the data to be saved
* @param array|\ArrayObject $options
* @return bool|Entity false if $entity could not be saved, otherwise it returns
* the saved entity
* @see Table::save()
*/
public abstract function save(Entity $entity, array $options = []);
public abstract function saveAssociated(Entity $entity, array $options = []);

}
2 changes: 1 addition & 1 deletion src/ORM/Association/BelongsTo.php
Expand Up @@ -115,7 +115,7 @@ public function type() {
* the saved entity
* @see Table::save()
*/
public function save(Entity $entity, array $options = []) {
public function saveAssociated(Entity $entity, array $options = []) {
$targetEntity = $entity->get($this->property());
if (empty($targetEntity) || !($targetEntity instanceof Entity)) {
return $entity;
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -382,7 +382,7 @@ public function saveStrategy($strategy = null) {
* @see Table::save()
* @see BelongsToMany::replaceLinks()
*/
public function save(Entity $entity, array $options = []) {
public function saveAssociated(Entity $entity, array $options = []) {
$targetEntity = $entity->get($this->property());
$strategy = $this->saveStrategy();

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/HasMany.php
Expand Up @@ -72,7 +72,7 @@ public function isOwningSide(Table $side) {
* @see Table::save()
* @throws \InvalidArgumentException when the association data cannot be traversed.
*/
public function save(Entity $entity, array $options = []) {
public function saveAssociated(Entity $entity, array $options = []) {
$targetEntities = $entity->get($this->property());
if (empty($targetEntities)) {
return $entity;
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/HasOne.php
Expand Up @@ -110,7 +110,7 @@ public function type() {
* the saved entity
* @see Table::save()
*/
public function save(Entity $entity, array $options = []) {
public function saveAssociated(Entity $entity, array $options = []) {
$targetEntity = $entity->get($this->property());
if (empty($targetEntity) || !($targetEntity instanceof Entity)) {
return $entity;
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Associations.php
Expand Up @@ -229,7 +229,7 @@ protected function _save($association, $entity, $nested, $options) {
if (!empty($nested)) {
$options = (array)$nested + $options;
}
return (bool)$association->save($entity, $options);
return (bool)$association->saveAssociated($entity, $options);
}

/**
Expand Down
26 changes: 13 additions & 13 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -199,7 +199,7 @@ public function testSaveStrategy() {
}

/**
* Tests that it is possible to pass the save strategy in the constructor
* Tests that it is possible to pass the saveAssociated strategy in the constructor
*
* @return void
*/
Expand Down Expand Up @@ -1240,7 +1240,7 @@ public function testReplaceWithMissingPrimaryKey() {
}

/**
* Test that replaceLinks() can save an empty set, removing all rows.
* Test that replaceLinks() can saveAssociated an empty set, removing all rows.
*
* @return void
*/
Expand Down Expand Up @@ -1415,7 +1415,7 @@ public function testReplaceLinkSuccess() {
*
* @return void
*/
public function testSaveEmptySetSuccess() {
public function testSaveAssociatedEmptySetSuccess() {
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['_saveTarget', 'replaceLinks'],
Expand All @@ -1431,15 +1431,15 @@ public function testSaveEmptySetSuccess() {
->method('replaceLinks');
$assoc->expects($this->never())
->method('_saveTarget');
$this->assertSame($entity, $assoc->save($entity));
$this->assertSame($entity, $assoc->saveAssociated($entity));
}

/**
* Tests saving with replace strategy returning true
*
* @return void
*/
public function testSaveWithReplace() {
public function testSaveAssociatedWithReplace() {
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['replaceLinks'],
Expand All @@ -1457,15 +1457,15 @@ public function testSaveWithReplace() {
$assoc->expects($this->once())->method('replaceLinks')
->with($entity, $entity->tags, $options)
->will($this->returnValue(true));
$this->assertSame($entity, $assoc->save($entity, $options));
$this->assertSame($entity, $assoc->saveAssociated($entity, $options));
}

/**
* Tests saving with replace strategy returning true
*
* @return void
*/
public function testSaveWithReplaceReturnFalse() {
public function testSaveAssociatedWithReplaceReturnFalse() {
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['replaceLinks'],
Expand All @@ -1483,19 +1483,19 @@ public function testSaveWithReplaceReturnFalse() {
$assoc->expects($this->once())->method('replaceLinks')
->with($entity, $entity->tags, $options)
->will($this->returnValue(false));
$this->assertFalse($assoc->save($entity, $options));
$this->assertFalse($assoc->saveAssociated($entity, $options));
}

/**
* Test that save() ignores non entity values.
* Test that saveAssociated() ignores non entity values.
*
* @return void
*/
public function testSaveOnlyEntities() {
public function testSaveAssociatedOnlyEntities() {
$connection = ConnectionManager::get('test');
$mock = $this->getMock(
'Cake\ORM\Table',
['save', 'schema'],
['saveAssociated', 'schema'],
[['table' => 'tags', 'connection' => $connection]]
);
$mock->primaryKey('id');
Expand All @@ -1516,10 +1516,10 @@ public function testSaveOnlyEntities() {
]);

$mock->expects($this->never())
->method('save');
->method('saveAssociated');

$association = new BelongsToMany('Tags', $config);
$association->save($entity);
$association->saveAssociated($entity);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -270,18 +270,18 @@ public function testCascadeDelete() {
}

/**
* Test that save() ignores non entity values.
* Test that saveAssociated() ignores non entity values.
*
* @return void
*/
public function testSaveOnlyEntities() {
public function testSaveAssociatedOnlyEntities() {
$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
$config = [
'sourceTable' => $this->client,
'targetTable' => $mock,
];
$mock->expects($this->never())
->method('save');
->method('saveAssociated');

$entity = new Entity([
'title' => 'A Title',
Expand All @@ -290,7 +290,7 @@ public function testSaveOnlyEntities() {
]);

$association = new BelongsTo('Authors', $config);
$result = $association->save($entity);
$result = $association->saveAssociated($entity);
$this->assertSame($result, $entity);
$this->assertNull($entity->author_id);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -737,11 +737,11 @@ public function testCascadeDeleteCallbacks() {
}

/**
* Test that save() ignores non entity values.
* Test that saveAssociated() ignores non entity values.
*
* @return void
*/
public function testSaveOnlyEntities() {
public function testSaveAssociatedOnlyEntities() {
$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
$config = [
'sourceTable' => $this->author,
Expand All @@ -758,10 +758,10 @@ public function testSaveOnlyEntities() {
]);

$mock->expects($this->never())
->method('save');
->method('saveAssociated');

$association = new HasMany('Articles', $config);
$association->save($entity);
$association->saveAssociated($entity);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -272,18 +272,18 @@ public function testAttachToMultiPrimaryKeyMistmatch() {
}

/**
* Test that save() ignores non entity values.
* Test that saveAssociated() ignores non entity values.
*
* @return void
*/
public function testSaveOnlyEntities() {
public function testSaveAssociatedOnlyEntities() {
$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
$config = [
'sourceTable' => $this->user,
'targetTable' => $mock,
];
$mock->expects($this->never())
->method('save');
->method('saveAssociated');

$entity = new Entity([
'username' => 'Mark',
Expand All @@ -292,7 +292,7 @@ public function testSaveOnlyEntities() {
]);

$association = new HasOne('Profiles', $config);
$result = $association->save($entity);
$result = $association->saveAssociated($entity);

$this->assertSame($result, $entity);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/AssociationTest.php
Expand Up @@ -52,7 +52,7 @@ public function setUp() {
'\Cake\ORM\Association',
[
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'save', 'eagerLoader', 'type'
'saveAssociated', 'eagerLoader', 'type'
],
['Foo', $config]
);
Expand Down
24 changes: 12 additions & 12 deletions tests/TestCase/ORM/AssociationsTest.php
Expand Up @@ -175,13 +175,13 @@ public function testSaveParents() {
$table = $this->getMock('Cake\ORM\Table', [], [[]]);
$mockOne = $this->getMock(
'Cake\ORM\Association\BelongsTo',
['save'],
['saveAssociated'],
['Parent', [
'sourceTable' => $table,
]]);
$mockTwo = $this->getMock(
'Cake\ORM\Association\HasMany',
['save'],
['saveAssociated'],
['Child', [
'sourceTable' => $table
]]);
Expand All @@ -196,12 +196,12 @@ public function testSaveParents() {
$options = ['option' => 'value'];

$mockOne->expects($this->once())
->method('save')
->method('saveAssociated')
->with($entity, $options)
->will($this->returnValue(true));

$mockTwo->expects($this->never())
->method('save');
->method('saveAssociated');

$result = $this->associations->saveParents(
$table,
Expand All @@ -221,13 +221,13 @@ public function testSaveParentsFiltered() {
$table = $this->getMock('Cake\ORM\Table', [], [[]]);
$mockOne = $this->getMock(
'Cake\ORM\Association\BelongsTo',
['save'],
['saveAssociated'],
['Parents', [
'sourceTable' => $table,
]]);
$mockTwo = $this->getMock(
'Cake\ORM\Association\BelongsTo',
['save'],
['saveAssociated'],
['Categories', [
'sourceTable' => $table
]]);
Expand All @@ -242,12 +242,12 @@ public function testSaveParentsFiltered() {
$options = ['atomic' => true];

$mockOne->expects($this->once())
->method('save')
->method('saveAssociated')
->with($entity, ['atomic' => true, 'associated' => ['Others']])
->will($this->returnValue(true));

$mockTwo->expects($this->never())
->method('save');
->method('saveAssociated');

$result = $this->associations->saveParents(
$table,
Expand All @@ -267,13 +267,13 @@ public function testSaveChildrenFiltered() {
$table = $this->getMock('Cake\ORM\Table', [], [[]]);
$mockOne = $this->getMock(
'Cake\ORM\Association\HasMany',
['save'],
['saveAssociated'],
['Comments', [
'sourceTable' => $table,
]]);
$mockTwo = $this->getMock(
'Cake\ORM\Association\HasOne',
['save'],
['saveAssociated'],
['Profiles', [
'sourceTable' => $table
]]);
Expand All @@ -288,12 +288,12 @@ public function testSaveChildrenFiltered() {
$options = ['atomic' => true];

$mockOne->expects($this->once())
->method('save')
->method('saveAssociated')
->with($entity, $options + ['associated' => ['Other']])
->will($this->returnValue(true));

$mockTwo->expects($this->never())
->method('save');
->method('saveAssociated');

$result = $this->associations->saveChildren(
$table,
Expand Down

0 comments on commit 3df2a07

Please sign in to comment.